views:

47

answers:

2

I'm writing a makefile that compiles a .java file in a different directory, and then I want to run it without changing directories. I want to do something along the lines of:

$(SQM_JAVA_TOOL_DONE) : $(SQM_JAVA_TOOL)
        $(shell cd /home_dir)
        javac myjavafile.java
        java myjavafile

where the java file is /home/myjavafile.java the makefile isn't running from /home. Any suggestions?

A: 

I might be misunderstanding the question, but you can compile with

javac /home/MyJavaFile.java

This will create MyJavaFile.class in /home

You can then run it by including /home on the classpath. e.g.

java -cp /home MyJavaFile

If you want to generate the class file in a different directory then you can use the -d option to javac.

mikej
+1  A: 

Use the -d command line parameter with javac to tell it what directory you'd like to store the compiled class files in. Then, to run the program, simply include this directory in the classpath:

javac -d some/directory myjavafile.java
java -cp some/directory myjavafile
Michael Angstadt