简体   繁体   中英

Compile and run with javac and java using classpath classes in jar

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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