bash 如何从命令行运行 intellij Java 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27953146/
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-09-18 12:11:18  来源:igfitidea点击:

How do i run an intellij Java project from command line?

javabashintellij-ideaidedevelopment-environment

提问by David T.

In Intellij, when i run a java project (via CTRL+SHIFT+F10) on the class that has a public static void mainmethod, the project runs as expected. Now, i want to run this same file/project, but on the command line. Furthermore, i want to be able to update this file and re-run it.

在 Intellij 中,当我在具有public static void main方法的类上运行 java 项目(通过 CTRL+SHIFT+F10)时,该项目按预期运行。现在,我想在命令行上运行这个相同的文件/项目。此外,我希望能够更新此文件并重新运行它。

I copied the command that Intellij outputs into the console, and i stuck that command on the terminal. the command looks something like this:

我将 Intellij 输出的命令复制到控制台中,并将该命令粘贴在终端上。命令看起来像这样:

/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14 CE.app/Con...... (it's an insanely long command)

/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/bin/java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14 CE.app/Con......(这是一个非常长的命令)

Let's say my file looks like this:

假设我的文件如下所示:

public class Main {
    public static void main(String...args){System.out.println("hi");}
}

==> outputs "hi" when i run the command.

==> 当我运行命令时输出“hi”。

While this command runs just fine on terminal, it does NOT pick up any updates to the file. if i save the file, the previous cached files are run.

虽然此命令在终端上运行得很好,但它不会获取文件的任何更新。如果我保存文件,则运行以前的缓存文件。

i.e. let's say now i modify the file to be:

即让我们说现在我将文件修改为:

public class Main {
    public static void main(String...args){System.out.println("GOOD BYE");}
}

==> the output is still "hi". even if i save the file.

==> 输出仍然是“嗨”。即使我保存了文件。

I'm running the command from home directory.

我正在从主目录运行命令。

How do i get the command to pick up my changes?

我如何获得命令来获取我的更改?

If i run again from intellij with the updated file, it gives the same command as previously (well, other than the port changes).

如果我使用更新后的文件再次从 intellij 运行,它会提供与以前相同的命令(好吧,除了端口更改)。

========================================================================

================================================== ======================

Update:

更新:

okay, it looks like the command is running just a .class file. But i still can't see the commands used to build the .class file. how do i do this?

好的,看起来该命令只是在运行一个 .class 文件。但是我仍然看不到用于构建 .class 文件的命令。我该怎么做呢?

回答by Peter Lawrey

You need to rebuild the project before running it if you want to see your changes. You can use javacbut I suggest you use a build tool like maven or gradle.

如果要查看更改,则需要在运行之前重建项目。您可以使用,javac但我建议您使用 maven 或 gradle 之类的构建工具。

Once you have rebuilt your project you can run the latest build.

重建项目后,您可以运行最新版本。

You can also create a script which first compiles your program before it runs it like IntelliJ does.

您还可以创建一个脚本,它首先编译您的程序,然后像 IntelliJ 一样运行它。