2

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)
Community
  • 1
  • 1
dialex
  • 2,706
  • 8
  • 44
  • 74

3 Answers3

4

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
wjans
  • 10,009
  • 5
  • 32
  • 43
  • 2
    Yes you can do that. Note that the <> should not be used, it was just to highlight that you should replace it with the path to your actual jar file. I suppose it would be something like this in your case: `java -cp ../lib/jansi.jar temp.Test` – wjans Jun 09 '11 at 11:44
  • If you have two jars, one in lib and one in bin, calling {{java -cp ..\lib\jansi.jar -jar yourjar.jar}} from bin should work. Scooped by wjans, though. :) – Urs Reupke Jun 09 '11 at 11:45
  • @wjans: yes, I know about the <> :P @Urs: Thx for the tip of two jars! I forgot the `..` – dialex Jun 09 '11 at 12:41
  • @jwans: I tried your solution but got another error `<...\SandBox\bin> java -cp ..\lib\jansi-1.4.jar temp.Test Exception in thread "main" java.lang.NoClassDefFoundError: temp/Test Caused by: java.lang.ClassNotFoundException: temp.Test at java.net.URLClassLoader$1.run(Unknown Source) ` – dialex Jun 09 '11 at 13:01
  • Oh, the current folder is not on your classpath anymore. Try adding the current folder "." to your classpath as well. Try it like this: `java -cp .;..\lib\jansi-1.4.jar temp.Test`. I assume you're running on a windows system, if not use ":" instead of ";" as a delimiter. – wjans Jun 09 '11 at 13:14
  • I'm on windows. I tried the `;` and it didn't work whereas `:` did work. But the error still remains. I uploaded the eclipse project to this [link](http://dl.dropbox.com/u/1862596/SandBox.zip) – dialex Jun 09 '11 at 17:07
  • What do you mean by "I tried the `;` and it didn't work"? It should work fine. Maybe you can put the classpath between quotes to be on the safe side. `java -cp ".;..\lib\jansi-1.4.jar" temp.Test` – wjans Jun 10 '11 at 04:48
  • The quotes didn't work, I got another error, please check my edit at the original question. – dialex Jun 13 '11 at 13:46
  • In your edit you don't have -cp anymore. If you specify it like this it should work: `java -cp ".;..\lib\jansi-1.4.jar" temp.Test`. Tried it with and without quotes, forward/backward slashes, ... it all works. – wjans Jun 14 '11 at 05:00
1

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

or better classpath

Tioma
  • 2,120
  • 9
  • 35
  • 52
0

[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.

dialex
  • 2,706
  • 8
  • 44
  • 74