Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

javac - How can I compile and run a Java class in a different directory?

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, and the makefile isn't running from /home.

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...