如何在不同的目录中编译和运行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 $ c中创建
MyJavaFile.class
$ c>
This will create MyJavaFile.class
in /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屋!