Search code examples
javacommand-linesystem

IntelliJ: cannot launch a java application using the command line


Let me summarize the problem:

  • An exercise asked me to launch a java application using the Windows 10 Command Line.
  • It taught me on a video how to add the IntelliJ path to the Environment Variables, so that, whenever I typed java Main I would get my Main.java/class application back.
  • The first problem that I had stumbled upon was that, instead of using java Main, I'd have to type java Main.java. I thought that it was due to a recent build for the JDK (v14.02), so I let that one pass.
  • The second problem that I had encountered was that, when the exercise asked me to launch an application using the package directories, the command line would return the following error: Error: Could not find or load main class com.pluralsight.organized.Main. Caused by: java.lang.ClassNotFoundException: com.pluralsight.organized.Main.

Solution

    1. Revert whatever damage you've done to the system.
    2. Install the Oracle JDK.
    3. Put the Oracle JDK bin folder on your PATH variable.
    4. Compile your code using javac Main.java
    5. Run your compiled code using java Main
    6. for the ones in the packages, you need to be at the package root, or have a class path to it. For example, let's consider a class com.example.Main

    The folder structure should be: D:/SomePath/com/example/Main.class Then you need to be at D:/SomePath/ to execute java com.example.Main. Alternatively, you could be anywhere else and execute java -cp D:/SomePath com.example.Main.