简体   繁体   English

是否需要从像IntelliJ这样的IDE编译整个程序才能从命令行运行?

[英]Is It necessary to compile entire program from an IDE like IntelliJ to run from command line?

Say I setup a program in intelliJ in java and I have multiple classes and everything setup. 假设我在intelliJ中使用Java设置了一个程序,并且我有多个类,并且进行了所有设置。 I am trying to run this program from the command line, but I keep seeing in tutorials they are using the command javac programname.java and they are compiling the program and then running it. 我试图从命令行运行该程序,但是在教程中我不断看到他们使用命令javac programname.java并正在编译程序然后运行它。

But I have multiple classes and I'm using intelliJ to do everything. 但是我有多个类,并且正在使用intelliJ来做所有事情。 Do I just compile it from intellij or what's the best way to do this? 我只是从intellij编译它,还是最好的方法是什么? Do I even need to compile it, or is it all ready compiled? 我什至需要编译它,还是全部准备好编译?

I just want to be able to run my main class from the command line. 我只希望能够从命令行运行我的主类。

You do not need to compile it. 您不需要编译它。 Just run your main class and IDE will do the rest. 只需运行您的主类,其余的工作将由IDE完成。

Do remember to add required parameters to you main program, if any. 请记住要在主程序中添加必需的参数(如果有)。

This link would be helpful https://www.jetbrains.com/idea/help/running-applications.html 该链接将很有帮助https://www.jetbrains.com/idea/help/running-applications.html

You should think of the main method() as the entry point in your program. 您应该将main method()视为程序的入口点。 In other words, main() is the method that starts your program. 换句话说, main()是启动程序的方法。 So, when you are adding other classes intellij just adds imports to your other classes just like when your importing from the api. 因此,当您添加其他类时,IntelliJ只会将导入添加到其他类中,就像从api导入时一样。 So, assuming that your imports are correct you need to take the following steps. 因此,假设您的导入正确无误,则需要执行以下步骤。

  1. Compile all of your .java files (including the file with main which is sometimes referred to as a test client) using javac myFile.java 使用javac myFile.java编译所有.java文件(包括主文件,有时也称为测试客户端)。

  2. Run your compiled, main class with main method() by using java myClass 通过使用java myClass使用main method()运行已编译的主类

Edit: you must ensure that you add the location of your .class file to your classpath. 编辑:您必须确保将.class文件的位置添加到类路径中。 So, if its in the current folder then add . 因此,如果它在当前文件夹中,请添加。 to your classpath. 到你的classpath。 Note that the windows classpath separator is a semi-colon ie ; 请注意,windows类路径分隔符是分号,即;

Then you can use java -cp to compile and run 然后,您可以使用java -cp进行编译和运行

javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main

You can make an executable jar : 您可以创建一个可执行jar:

More information : http://www.mkyong.com/java/how-to-make-an-executable-jar-file/ 详细信息: http : //www.mkyong.com/java/how-to-make-an-executable-jar-file/

Intellij : How to build jars from IntelliJ properly? Intellij: 如何从IntelliJ正确构建jars?

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

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