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

拈花ヽ惹草 提交于 2019-11-27 07:48:21

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.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!