简体   繁体   English

涉及从 jar 文件导入的类路径问题

[英]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我遇到了与此处提到的相同的问题: exception-in-thread-main-java-lang-noclassdeffounderror-wrong-name

I had no problems executing with eclipse but with terminal I got a NoClassDef.我使用 eclipse 执行没有问题,但是使用终端我得到了 NoClassDef。 Rising one folder and executing java <package-name>.<class-name> worked perfectly.上升一个文件夹并执行java <package-name>.<class-name>效果很好。

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 .我知道这段代码可以运行,因为我是从Jansi 的作者页面复制的。 It's a library to print in color on a windows' terminal.这是一个在 Windows 终端上进行彩色打印的库。 What do I need to do to run this class?我需要做什么才能运行这个 class? Help is much appreciated.非常感谢您的帮助。


[UDPATE: SOLUTION] [UDPATE:解决方案]

I was advised to create a jar of the application I was trying to test and then run that jar.有人建议我为我尝试测试的应用程序创建一个 jar,然后运行该 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.因此,我创建了 jar “jprinter”,其中包含我的所有文件(不是我使用的外部 jar)和测试 class。 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;其中lib是我和其他 jars 使用的文件夹; print.test is the package of the class Test which contains the main method. print.test是 class Test的 package 包含主要方法。


[outdated:] I tried executing by running java.:..\lib\jansi-1.4.jar temp.Test which gave me the following ouput: [过时:]我尝试通过运行java.:..\lib\jansi-1.4.jar temp.Test执行,它给了我以下输出:

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:我还尝试通过运行java ".:..\lib\jansi-1.4.jar" temp.Testjava ".;..\lib\jansi-1.4.jar" temp.Test ,它给了我以下输出:

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)

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 ";":在 Unix 系统上应使用“:”,在 Windows 系统上应使用“;”:

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

You must specify -classpath where "org.fusesource.jansi.Ansi" is found.您必须指定找到“org.fusesource.jansi.Ansi”的 -classpath。 See for detail classpath有关详细信息,请参阅 类路径

or better classpath或更好的类路径

[UDPATE: SOLUTION] [UDPATE:解决方案]

I was advised to create a jar of the application I was trying to test and then run that jar.有人建议我为我尝试测试的应用程序创建一个 jar,然后运行该 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.因此,我创建了 jar “jprinter”,其中包含我的所有文件(不是我使用的外部 jar)和测试 class。 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;其中lib是我和其他 jars 使用的文件夹; print.test is the package of the class Test which contains the main method. print.test是 class Test的 package 包含主要方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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