java.lang.NoClassDefFoundError.在运行时找不到类. [英] java.lang.NoClassDefFoundError. Class not found during runtime.

查看:482
本文介绍了java.lang.NoClassDefFoundError.在运行时找不到类.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Eclipse,并且需要使用外部库.例如Jsoup和JXL.

I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.

现在为止我要做的是:首先在我的项目文件夹中创建一个"lib"文件夹.然后在eclipse中,单击项目属性,库"选项卡,添加外部jar,并将该jar添加到lib文件夹中.

Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.

所以这解决了我的编译问题.现在,当我运行程序时(我进入project/bin并在控制台中执行:java ProgramName;得到

So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get

java.lang.NoClassDefFoundError: 

现在要进行测试,我将Jar文件添加到Main.java所在的文件夹中,现在,我已经能够执行以下操作:

Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:

javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main  

这有效.

所以首先想到的是我必须告诉java在哪里可以找到相应的库.如果这是正确的?我该怎么办?

So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?

java -cp ???(dont know what to put here)

但是,此外.我还有另一个问题.我正在用计算机编写此程序,但是我将在可能没有这些库的其他程序中使用它.我该如何解决这个问题?

But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?

推荐答案

,如果您具有以下结构:

if you have this structure:

project folder
... code
... libs

然后从代码文件夹中:

javac -cp .;../libs/*.jar yourmainclass.java
java -cp  .;../libs/*.jar yourmainclass

当您需要编译并运行该项目时,请取出所有文件夹,并在另一台计算机上执行相同的操作.

When you need to compile and run this project, take all the folder and do the same in other machine.

这篇关于java.lang.NoClassDefFoundError.在运行时找不到类.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆