可执行jar文件错误 [英] Executable jar file error

查看:236
本文介绍了可执行jar文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在开发java应用程序。
我完成了编码部分,但是当我运行它时,我在线程主java.lang.noclassdeffounderror 中收到了异常。我通过互联网搜索了这个,但找不到合适的答案。



让我解释一下我在从eclipse导出java程序到可执行jar文件之前所做的事情: / p>


  • 我在库中添加了一些jar文件,因此我的程序可以连接第三方程序。它从一个名为SSC的自定义Jre运行,
    在第三方程序(SunSystems)位置文件夹中获得位置。


  • 我编写了运行该程序所需的java代码。


  • 然后我跑了该项目,当我从eclipse运行时效果很好。


  • 我点击了项目的名称然后点击了导出。我选择Runnable jar文件并将所需的文件复制到子文件夹中,接下来
    生成jar文件。我从启动
    配置中选择了正确的类。然后点击完成。




当我通过输入 java -jar从cmd运行jar文件时ssc.jar 它给了我这个错误:

 线程主java.lang.noclassdeffounderror $中的异常b $ b  

以及带标记的其他一些行。



我该怎么做才能解决这个问题?



有些文件说我需要添加类路径来编程,或编辑清单文件等。我无法弄清楚如何。



感谢您的帮助。谢谢



(编辑)演示包下的SSC.java类。当我从日食运行时它起作用:

  package demo; 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import com.systemsunion.ssc.client。*;

公共类SSC {

static String HOST =localhost;
static int Port = 8080;

public static void main(String [] args){
try
{
SecurityProvider secman = new SecurityProvider(HOST,true);
String voucher = secman.Authenticate(PKP,)。toString();
String sInputPayload =;
String path =C:/ SSC temp / temp.txt;
BufferedReader reader = new BufferedReader(new FileReader(path));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(C:/ SSC temp / temp-result.txt)));
String line =;
while((line = reader.readLine())!= null)
{
sInputPayload = sInputPayload + line;
}
尝试
{
SoapComponent ssc = new SoapComponent(HOST,Port);
ssc.authenticate(凭证);
String result = ssc.execute(Journal,Import,sInputPayload);
writer.write(result);
writer.newLine();
writer.close();
}
catch(exception ex)
{
ex.printStackTrace();
}
}
catch(exception ex)
{
ex.printStackTrace();
}
}
}

运行jar时出错来自cmd的文件:

 线程main中的异常java.lang.NoClassDefFoundError:com / systemsunion / ssc / 

client / SoapComponent
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class。 getMethod0(未知来源)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(未知来源)
引起:java.lang.ClassNotFoundException:com.systemsunion.ssc.client.SoapCom
ponent
at java.net.URLClassLoader $ 1.run(Unknown Source)
在java.net.URLClassLoader $ 1.run(未知来源)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher $ AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6更多


解决方案

问题在于将所需的库复制到生成的JAR旁边的子文件夹。执行此操作时,库将复制到与创建的jar文件相同的文件夹中的子文件夹。当您尝试运行jar文件时,必须将此子文件夹的所有jar添加到类路径中(我不确定通配符是否有效 - 我认为它们将使用Java 7):

  java -cp sub\jar1.jar; sub \ jar2.jar -jar ssc.jar 

为了避免这个问题(必须添加所有jar文件很麻烦),你可以使用以下任何一个:




  • 将所需的库提取到生成的JAR

  • 将所需的包打包到生成的JAR中



优点是:您的应用程序JAR现在将随内置所有依赖项一起运行,因此它始终有效。 缺点是: JAR内部存在依赖关系(可能导致一个非常大的JAR文件,其中可能只有一小部分是您的实际应用程序)。



编辑:您可能还需要在上面的命令行中添加对项目的引用。有关详细信息,请参阅此答案的评论。


Hey there i am developing java application. I completed coding part but i am recieving exception in thread main java.lang.noclassdeffounderror when i ran it. I have searched this through the internet, and could not find suitable answer.

Let me explain what i have done before exporting java program to the executable jar file from eclipse:

  • I added some jar files to my library so my program could connect a 3rd party program. It runs from a custom Jre which is named SSC and got location in 3rd party program's(SunSystems) location folder.

  • I wrote the required java code to run the program.

  • Then i ran the project, which works perfectly when i run from eclipse.

  • I clicked on project's name then clicked Export. I selected Runnable jar file and Copy required libraries into sub-folder next to generated jar file. I picked correct class from launch configuration. Then clicked finish.

When i ran the jar file from cmd by typing java -jar ssc.jar It gives me this error:

exception in thread main java.lang.noclassdeffounderror

and some other lines with tag.

What should i do to fix this?

Some documentations say that i need add classpath to program, or edit manifest file etc. I could not figure out how.

I would appreciate your help. Thanks

(Edit)SSC.java class under the demo package. It works when i run from the eclipse:

package demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import com.systemsunion.ssc.client.*;

public class SSC {

    static String HOST="localhost";
    static int Port=8080;

    public static void main(String[] args) {
        try
        {
            SecurityProvider secman= new SecurityProvider(HOST,true);           
            String voucher= secman.Authenticate("PKP","").toString();
            String sInputPayload="";
            String path="C:/SSC temp/temp.txt";
            BufferedReader reader= new BufferedReader(new FileReader(path));
            BufferedWriter writer= new BufferedWriter(new FileWriter(new File("C:/SSC temp/temp-result.txt")));
            String line="";
            while((line = reader.readLine()) != null)
            {
                sInputPayload = sInputPayload + line;
            }
            try
            {
                SoapComponent ssc= new SoapComponent(HOST, Port);
                ssc.authenticate(voucher);
                String result= ssc.execute("Journal", "Import", sInputPayload);
                writer.write(result);
                writer.newLine();
                writer.close();
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Error when i ran the jar file from cmd:

Exception in thread "main" java.lang.NoClassDefFoundError: com/systemsunion/ssc/

    client/SoapComponent
            at java.lang.Class.getDeclaredMethods0(Native Method)
            at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
            at java.lang.Class.getMethod0(Unknown Source)
            at java.lang.Class.getMethod(Unknown Source)
            at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
            at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: com.systemsunion.ssc.client.SoapCom
    ponent
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 6 more

解决方案

The problem is with "Copy required libraries into a sub-folder next to the generated JAR". When you do this, the libraries are copied to a sub-folder in the same folder as the created jar file. When you attempt to run the jar file, you must add all the jars this sub-folder to the classpath (I'm not sure if wildcards work - I think they will with Java 7):

java -cp sub\jar1.jar;sub\jar2.jar -jar ssc.jar

To avoid this issue (having to add all the jar files is cumbersome), you can use either of these:

  • Extract required libraries into generated JAR
  • Package required libraries into generated JAR

The advantage is: your application JAR will now travel with all dependencies built-in so it will always work. The disadvantage is: a JAR bloated with the dependencies inside (potentially resulting in a very large JAR file, out of which maybe only a small part is your actual application).

Edit: You may also need to add a reference to your project in the command line above. See comments on this answer for more details.

这篇关于可执行jar文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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