1

I'm trying to run a simple code in java by command prompt. I have a jar.java file containing this :

public class jar{

public void print()
{
System.out.println("Jar success accesing !");
}}

and also i have a main.java that call use jar class:

public class main{

public static void main (String args[]){
jar jar1 = new jar();
jar1.print();
}}

I just tried to compile jar.java and then make it an jar archive, and then compile main class using this jar like here :

>javac jar.java
>jar cvf JAR.jar jar.class
>javac -cp JAR.jar main.java

Now, until here all works fine bun when i want to run main it doesn't work:

>java -cp JAR.jar main

and i get this :

Error: Could not find or load main class main.class
Caused by: java.lang.ClassNotFoundException: main.class

Where i am wrong ?

  • what is the package of these two classes? (maybe default, that is, they have no package?) – Daniele Jun 11 '18 at 21:43
  • JAR.jar just has jar.class not the main.class. See your jar command. When you just have two classes, you shouldn't get this confused :) By the way follow java naming convention. – gagan singh Jun 11 '18 at 21:48
  • you are so far ! of course i have just jar.class inside, this class i want to call from an external main class. the same files are in the same package – Giurgiu Aron Jun 11 '18 at 21:59

1 Answers1

0

The answer is:

java  main

You don't need -cp because you put all files in the same folder. Your code don't work because you override the default value of -cp (current directory). Your -cp point only to JAR.jar.

Similar solutions:

java -cp 'main.class;JAR.jar' main
java -cp '.;JAR.jar' main
Blocked
  • 340
  • 2
  • 5
  • 20