简体   繁体   English

如何从控制台而不是从IntelliJ运行已编译的Java项目

[英]How to run compiled java project from console, not from IntelliJ

After the the demo project is compiled, there are many .class file in the out>production>testPrj>apidemo. 演示项目编译后,out> production> testPrj> apidemo中有许多.class文件。 Basically, each file will have one .class file 基本上,每个文件都有一个.class文件

I expect to enter the console: 我希望进入控制台:

java apidemo.class java apidemo.class

but it doesn't work. 但这不起作用。

I tried "java apidemo.class". 我尝试了“ java apidemo.class”。 The error msg is "Error: Could not find or load main class apidemo". 错误消息是“错误:无法找到或加载主类apidemo”。

I also read this post . 我也读了这篇文章 It is not working for my situation. 它不适用于我的情况。 My compile is success, and it can be run from Intellj, but I don't know how to run it from console. 我的编译成功了,它可以从Intellj运行,但是我不知道如何从控制台运行。 How do I run a compiled java project from console? 如何从控制台运行已编译的Java项目?

在此处输入图片说明

For running from console you have to do few things: 要从控制台运行,您需要做一些事情:

  • to be sure that your class apidemo.ApiDemo has main() for lunching your program. 确保您的类apidemo.ApiDemo具有main()来午餐程序。
  • compile sources - navigate to folder where is source file located (it has already compiled by Intellij): 编译源代码-导航至源文件所在的文件夹(Intellij已对其进行编译):

    javac ApiDemo.java javac ApiDemo.java

  • run compiled files with .class extension, providing full class name (with packages): 运行扩展名为.class编译文件,并提供完整的类名 (带有包):

    java apidemo.ApiDemo java apidemo.ApiDemo

You need to provide the fully qualified name of the class with package name and not include ".class". 您需要使用包名称提供类的完全限定名称,并且不包括“ .class”。 So you need to place yourself in the parent directory to where ApiDemo.class is - ie out>production>testPrj. 因此,您需要将自己放置在ApiDemo.class所在的父目录中-即out> production> testPrj。

And then execute: 然后执行:

$ java apidemo.ApiDemo

Another way is to provide "out/production/testPrj" as the classpath: 另一种方法是提供“ out / production / testPrj”作为类路径:

$ java -cp /path/to/out/production/testPrj apidemo.ApiDemo

If the class is in a package: 如果该类在包中:

package mypackagename;

public class MyClassName {
  public static final void main(String[] cmdLineParams)  {
  } 
}

You need to use: 您需要使用:

java -classpath . MyClassName

Notice the "." 注意“。”
It must be called with its fully-qualified name: 必须使用完全限定的名称来调用它:

java -classpath . mypackagename.MyClassName

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

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