5

Has anyone successfully created a NetBeans project that combines Clojure and Java source?

I have projects where the driver program (startup, gui, user prefs, etc.) are in Java, but the logic is in Clojure. At the moment, I compile the Clojure code to a jar in one project and import it as a library in a separate Java project. It would be convenient if all the source could be combined in one single NetBeans project.

Has anyone come up with a method to do this?

1
  • I've not done this in Netbeans but have it working in Eclipse so the same approach may work: set everything up as a Java project, have the clojure jars in the classpath and include your .clj files in the build so that they are available on the classpath at runtime. Commented Jul 9, 2011 at 11:17

2 Answers 2

2

One possible solution is to modify your NetBeans Java project's Ant script (build.xml in your root directory) to have it .

By default, NetBeans creates several placeholder Ant targets in the root project directory's build.xml for you to override to automate tasks beyond the standard build process (such as compiling other languages to use their libraries in your current project). By overriding one of the placeholder targets in that build script such as "-pre-compile" you could write a simple target to call the Clojure compilation process using the Ant "exec" task and place all the resulting class files (or JAR) in the appropriate build directory.

If you do this frequently, you could define an Ant extension (via a macro or Ant plugin) so you don't have to modify the build.xml each time.

Sign up to request clarification or add additional context in comments.

3 Comments

What a great idea!. I've used the placeholder targets for other purposes in the past, but this never occurred to me.
Glad it worked well for you! I actually got the idea because we NEED to use Ant as a build script language for a proprietary language my company designed (unless we wanted to compile/link every source file by hand). Writing our build tools in Ant let's us integrate this otherwise brittle, intermediate language with all of Eclipse/NetBeans Java projects.
Can you provide an example?
1

I use the RT method. I put my Clojure code into a script file that I include and process at startup:

try {
            RT.loadResourceScript("com/mydomain/app/clojure_scripts.clj"); // Initialize Clojure script processor with our script
        } catch (Exception e) {
            Util.logException(e, "Unable to run Clojure initialization script.");
        }

Then, since my main logic is in Java and I'm only calling out to Clojure for calculations, I use some glue code to map the calls for me:

/*
* Class to wrap Clojure scripts with Java friendly methods.
 */
public class Clojure {
    private static final String ns="com.mydomain.app";

    public static double calculate(final double size, final double otherVar) {
        Var report=RT.var(ns, "calculate");
        return (Double) report.invoke(size, otherVar);
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.