0

I have the following .java file:

import java.io.*;
import org.apache.commons.cli.*;

    public class Main 
    {
        public static void main(String[] args)
        {
           ...
        }
    }

I am compiling it with: javac -cp ./commons-cli-1.4.jar Main.java But when I try to run it with: java -cp ./commons-cli-1.4.jar Main I get the following error: Could not find or load main class Main. I know I should give the full class name after the path to the jar file but I do not have any package. All I have on this machine about this project is one directory including .jar file and Main.java and already Main.class. So, I cannot understand what could be the problem...

barni
  • 49
  • 1
  • 2
  • 7
  • You need to include the current directory in the classpath as well. Now the classpath only has the jar file, which doesn't contain `Main`. – Kayaman Jun 22 '17 at 13:22
  • I have tried `java -cp /home/myDir/commons-cli-1.4.jar Main` and also `java -cp /home/myDir/commons-cli-1.4.jar /home/myDir/Main` but it still returns the same error. – barni Jun 22 '17 at 13:26

1 Answers1

1

You have to add the actual Directory to the classpath too

Windows:

 java -cp ./;./commons-cli-1.4.jar Main.java

IX:

 java -cp ./:./commons-cli-1.4.jar Main.java
Jens
  • 67,715
  • 15
  • 98
  • 113