简体   繁体   English

Jar文件无法运行,提取的类运行良好

[英]Jar file fails to run, extracted class runs fine

I apoligize if this has been asked before but I have been unable to find anything that is the same. 我不知道以前是否曾问过这个问题,但是我找不到相同的东西。

I created a simple jar(myFailingJar.jar) file with two classes a main class and a simple class that accesses a class and its functions from another jar file (CCJAPI.jar). 我创建了一个简单的jar(myFailingJar.jar)文件,该文件具有两个类,一个是主类,另一个是从另一个jar文件(CCJAPI.jar)访问一个类及其功能的简单类。

The main class just instantiates the simple class, the simple class loads a shared library object and calls a function within CCJAPI.jar that crosses over JNI. 主类仅实例化简单类,简单类加载共享库对象并在CCJAPI.jar中调用跨越JNI的函数。

When run as a jar file with this command it fails because it can't find a class in CCJAPI.jar which is on the classpath: java -classpath /home/scott:/home/scott/CCJAPI.jar -jar myFailingJar.jar 当使用此命令作为jar文件运行时,它将失败,因为它无法在类路径上的CCJAPI.jar中找到一个类:java -classpath /home/scott:/home/scott/CCJAPI.jar -jar myFailingJar.jar

Starting 开始

Exception in thread "main" java.lang.NoClassDefFoundError: ccjni/DeviceManager
    at DetachedManager.DetachedDeviceManager.startManager(DetachedDeviceManager.java:24)
    at DetachedManager.Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: ccjni.DeviceManager
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

If I extract the contents of the myFailingJar.jar file and run with this command it works, which as far as I can tell just goes to the extracted class files and runs the : java -classpath /home/scott:/home/scott/CCJAPI.jar DetachedManager.Main 如果我提取myFailingJar.jar文件的内容并使用此命令运行,则它可以正常工作,据我所知,仅转到提取的类文件并运行:java -classpath / home / scott:/ home / scott / CCJAPI.jar DetachedManager.Main

Starting ** Started ** Success = - Going to crash now 正在开始**已开始**成功=-即将崩溃

Here is both source files contents: 这是两个源文件的内容:

Source of Main 主要来源

package DetachedManager;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        DetachedDeviceManager devMgr = new DetachedDeviceManager();
        if( devMgr.startManager() )
        {
            System.out.println("Success = - Going to crash now");
        }

    }

}

Source of simple class: 简单类的来源:

package DetachedManager;

import ccjni.DeviceManager;

public class DetachedDeviceManager {

    {
        System.load("/usr/lib/libccJNI.so");
    }

    public boolean startManager()
    {
        System.out.println("Starting");
        DeviceManager.start();
        System.out.println("** Started ** ");
        return true;
    }

}

The only difference is that one is tyring to run the compiled class from within the jar file and the other is running it outside the jar file. 唯一的区别是,一种方法是从jar文件内部运行已编译的类,另一种方法是在jar文件外部运行该类。 It must be some type of classpath or path issue that I have not been able to figure out. 这一定是我无法弄清楚的某种类路径或路径问题。 Any help would be much appreciated. 任何帮助将非常感激。

You probably are not creating the correct jar with the correct dependancies . 您可能没有使用正确的依赖关系创建正确的jar。 Use the eclipse export (Right click on a project --> Export ), This will also create the required manifest file. 使用eclipse导出(右键单击项目-> Export ),这还将创建所需的清单文件。

出口罐

Using this should be helpful. 使用它应该会有所帮助。

When I specified the classpath on the command line I thought that should be sufficient for any jar file loaded into the java environment to find a jar file, apparently it is not. 当我在命令行上指定类路径时,我认为对于装入Java环境的任何jar文件来说,找到一个jar文件应该足够了,显然不是。 I ended up having to add the class path into the manifest file even though that path was specified on the command line. 我最终不得不将类路径添加到清单文件中,即使该路径是在命令行上指定的也是如此。

Here is the failing manifest: 这是失败的清单:

Manifest-Version: 1.0
Created-By: 1.6.0_0 (Sun Microsystems Inc.)
Main-Class: DetachedManager.Main

Here is the successful manifest 这是成功的清单

Manifest-Version: 1.0
Created-By: 1.6.0_0 (Sun Microsystems Inc.)
Class-Path: lib/CCAPI.jar
Main-Class: DetachedManager.Main

Thanks to all for listening and giving me an idea of where and what to look at. 感谢所有人的聆听,并给我一个关于在哪里看什么的想法。

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

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