dialex
dialex

Reputation: 2876

Problem with classpath involving imports from a jar file

I was having the same problem as mentioned here: exception-in-thread-main-java-lang-noclassdeffounderror-wrong-name

I had no problems executing with eclipse but with terminal I got a NoClassDef. Rising one folder and executing java <package-name>.<class-name> worked perfectly.

My simple code is the following:

package temp;

import org.fusesource.jansi.Ansi;

public class Test {

    public static void main(String[] args) {

        System.out.println("I'm going to blow on the next line!");
        System.out.println( Ansi.ansi().eraseScreen().render("@|bold,red Hello|@ @|green World|@") );

    }

}

I know this code runs, because I copied it from the Jansi's author page. It's a library to print in color on a windows' terminal. What do I need to do to run this class? Help is much appreciated.


[UDPATE: SOLUTION]

I was advised to create a jar of the application I was trying to test and then run that jar. So I created the jar "jprinter" containing all my files (not the external jar I was using) and the test class with the main. After that I could execute in any folder

java -cp ".\lib\jprinter-1.15.jar;.\lib\*" print.test.Test

where lib is the folder with my and other jars used; print.test is the package of the class Test which contains the main method.


[outdated:] I tried executing by running java .:..\lib\jansi-1.4.jar temp.Test which gave me the following ouput:

I'm going to blow on the next line!
Exception in thread "main" java.lang.NoClassDefFoundError: org/fusesource/jansi/Ansi
        at temp.Test.main(Test.java:13)

And I also tried executing by running java ".:..\lib\jansi-1.4.jar" temp.Test or java ".;..\lib\jansi-1.4.jar" temp.Test which gave me the following ouput:

Exception in thread "main" java.lang.NoClassDefFoundError: \lib\jansi-1.4\jar
Caused by: java.lang.ClassNotFoundException: .:..\lib\jansi-1.4.jar
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

Upvotes: 2

Views: 2304

Answers (3)

dialex
dialex

Reputation: 2876

[UDPATE: SOLUTION]

I was advised to create a jar of the application I was trying to test and then run that jar. So I created the jar "jprinter" containing all my files (not the external jar I was using) and the test class with the main. After that I could execute in any folder

java -cp ".\lib\jprinter-1.15.jar;.\lib\*" print.test.Test

where lib is the folder with my and other jars used; print.test is the package of the class Test which contains the main method.

Upvotes: 0

wjans
wjans

Reputation: 10115

You should add this library to your classpath. Try running it like this:

java -cp .:<path_to_your_jansi_jar> temp.Test

Note that the delimiter is platform dependent. On a Unix system ":" should be used, on a Windows system it will be ";":

java -cp .;<path_to_your_jansi_jar> temp.Test

In your particular case it will probably be like this:

java -cp .;..\lib\jansi-1.4.jar temp.Test

Upvotes: 4

Tioma
Tioma

Reputation: 2150

You must specify -classpath where "org.fusesource.jansi.Ansi" is found. See for detail classpath

or better classpath

Upvotes: 1

Related Questions