如何在不同的目录中编译和运行 Java 类? [英] How can I compile and run a Java class in a different directory?
问题描述
我正在编写一个 makefile 来编译不同目录中的 .java
文件,然后我想在不更改目录的情况下运行它.我想做一些类似的事情:
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
其中 Java 文件是 /home/myjavafile.java
,并且 makefile 不是从 /home
运行的.
where the Java file is /home/myjavafile.java
, and the makefile isn't running from /home
.
我该怎么做?
推荐答案
我可能误解了这个问题,但你可以编译
I might be misunderstanding the question, but you can compile with
javac /home/MyJavaFile.java
这将在 /home
然后您可以通过在类路径中包含 /home
来运行它.例如
You can then run it by including /home
on the classpath. e.g.
java -cp /home MyJavaFile
如果你想在不同的目录中生成类文件,那么你可以使用 -d
选项到 javac
.
If you want to generate the class file in a different directory then you can use the -d
option to javac
.
这篇关于如何在不同的目录中编译和运行 Java 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!