如何在不同的目录中编译和运行 Java 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3415507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:07:47  来源:igfitidea点击:

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

javajavac

提问by vette982

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

我正在编写一个 makefile 来编译.java不同目录中的文件,然后我想在不更改目录的情况下运行它。我想做一些事情:

$(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.

Java 文件所在的位置/home/myjavafile.java,并且生成文件不是从/home.

How can I do this?

我怎样才能做到这一点?

采纳答案by mikej

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

我可能误解了这个问题,但你可以编译

javac /home/MyJavaFile.java

This will create MyJavaFile.classin /home

这将MyJavaFile.class/home

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

然后,您可以通过包含/home在类路径中来运行它。例如

java -cp /home MyJavaFile

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

如果你想生成一个不同的目录中的类文件,那么你可以使用该-d选项javac

回答by Michael

Use the -dcommand line parameter with javacto 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:

使用-d命令行参数 withjavac告诉它你想将编译的类文件存储在哪个目录中。 然后,要运行程序,只需将此目录包含在类路径中:

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

回答by dimo414

Just to add to the existing answers, you may want the --source-pathflag:

只是为了添加到现有答案中,您可能需要该--source-path标志:

--source-path <path>, -sourcepath <path>
      Specify where to find input source files
--source-path <path>, -sourcepath <path>
      Specify where to find input source files

I believe this effectively sets the package root javacwill compile from (i.e. <path>will be stripped from the expected package name of the files). It's still necessary to enumerate the files to compile, and this should still be relative to the current working directory, notthe path passed to --source-path.

我相信这有效地设置了包根javac将从中编译(<path>即将从文件的预期包名称中删除)。仍然需要枚举要编译的文件,并且这仍然应该是相对于当前工作目录的,而不是传递给--source-path.

For example, to compile and run from a project's root where source is stored in src/and you want it build in bin/:

例如,要从存储源的项目的根目录编译和运行,src/并且您希望它内置bin/

$ javac --source-path src -d bin src/mypackage/*.java
$ java -cp bin mypackage.Main

This works even from directories elsewhere in the filesystem, e.g.:

这甚至适用于文件系统中其他地方的目录,例如:

$ javac --source-path /some/absolute/path/src  -d /some/absolute/path/bin /some/absolute/path/
$ java -cp /some/absolute/path/bin mypackage.Main