简体   繁体   English

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

[英]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.我正在编写一个在不同目录中编译.java文件的生成文件,然后我想在不更改目录的情况下运行它。 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 . Java 文件是/home/myjavafile.java ,并且 makefile 不是从/home运行的。

How can I do this?我怎样才能做到这一点?

I might be misunderstanding the question, but you can compile with我可能误解了这个问题,但你可以编译

javac /home/MyJavaFile.java

This will create MyJavaFile.class in /home这将在/home创建MyJavaFile.class

You can then run it by including /home on the classpath.然后,您可以通过在类路径中包含/home来运行它。 eg例如

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 .如果要在不同的目录中生成类文件,则可以使用javac-d选项。

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:-d命令行参数与javac一起使用,告诉它您要将编译后的类文件存储在哪个目录中。然后,要运行该程序,只需将此目录包含在类路径中:

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

Just to add to the existing answers, you may want the --source-path flag:只是为了添加到现有答案中,您可能需要--source-path标志:

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

I believe this effectively sets the package root javac will compile from (ie <path> will be stripped from the expected package name of the files).我相信这有效地设置了包根javac将从编译(即<path>将从文件的预期包名称中剥离)。 It's still necessary to enumerate the files to compile, and this should still be relative to the current working directory, not the path passed to --source-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, eg:这甚至适用于文件系统中其他地方的目录,例如:

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

I am using VS Code and installed java and code runner extensions.我正在使用 VS Code 并安装了java代码运行器扩展。 When I created new java project using the extension, it was creating the .class file in src instead of bin.当我使用扩展创建新的 java 项目时,它是在 src 而不是 bin 中创建 .class 文件。 To solve the issue I opened settings.json file from File > Preferences > Settings and searched for "settings" (or "code-runner").为了解决这个问题,我从 File > Preferences > Settings 打开了 settings.json 文件并搜索了“settings”(或“code-runner”)。 Then I added following lines in that file.然后我在该文件中添加了以下几行。

"code-runner.executorMap": {
    "java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && java -cp bin $fileNameWithoutExt",
}

If you don`t want to see the command that runs before code file then add these lines instead:如果您不想看到在代码文件之前运行的命令,请添加以下行:

"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
"code-runner.executorMap": {
    "java": "there is && clear added in the execution paramater"
    "java": "cd \"$workspaceRoot\\\" && javac --source-path src -d bin src\\$fileName && clear && java -cp bin $fileNameWithoutExt",
}

I hope this finds someone with similar issue.我希望这能找到有类似问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM