How to run compiled java project from console, not from IntelliJ

前端 未结 3 850
广开言路
广开言路 2021-01-29 09:47

After the the demo project is compiled, there are many .class file in the out>production>testPrj>apidemo. Basically, each file will have one .class file

I expect to ente

相关标签:
3条回答
  • 2021-01-29 09:58

    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
    
    0 讨论(0)
  • 2021-01-29 10:04

    You need to provide the fully qualified name of the class with package name and not include ".class". So you need to place yourself in the parent directory to where ApiDemo.class is - i.e. out>production>testPrj.

    And then execute:

    $ java apidemo.ApiDemo
    

    Another way is to provide "out/production/testPrj" as the classpath:

    $ java -cp /path/to/out/production/testPrj apidemo.ApiDemo
    
    0 讨论(0)
  • 2021-01-29 10:05

    For running from console you have to do few things:

    • to be sure that your class apidemo.ApiDemo has main() for lunching your program.
    • compile sources - navigate to folder where is source file located (it has already compiled by Intellij):

      javac ApiDemo.java

    • run compiled files with .class extension, providing full class name (with packages):

      java apidemo.ApiDemo

    0 讨论(0)
提交回复
热议问题