提问者:小点点

我的程序在Eclipse中运行,但不是作为可执行jar。NoClassDefFoundError, ClassNotFoundException


我的项目将在Eclipse中运行没有问题,但是当导出为jar并从命令提示符(java-jar自动化. jar)运行时,程序崩溃并给我以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: autoitx4java/AutoItX
        at BuildCustomAction.main(BuildCustomAction.java:66)
Caused by: java.lang.ClassNotFoundException: autoitx4java.AutoItX
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

…和:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: C
:\Users\XXXX\Documents\lib\jacob-1.18-M2-x64.dll
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1827)
        at java.lang.Runtime.load0(Runtime.java:809)
        at java.lang.System.load(System.java:1086)
        at com.jacob.com.LibraryLoader.loadJacobLibrary(LibraryLoader.java:151)
        at com.jacob.com.JacobObject.<clinit>(JacobObject.java:110)
        at autoitx4java.AutoItX.<init>(AutoItX.java:181)
        at BuildCustomAction.main(BuildCustomAction.java:66)

这是完整的来源。

import java.io.File;
import autoitx4java.AutoItX;
import com.jacob.com.LibraryLoader;
import java.lang.System;
import java.util.UUID;
import javax.swing.JOptionPane;

public class BuildCustomAction {

    public static void main(String[] args) throws InterruptedException {

        // Save the user's %HOMEPATH% directory
        String homepath = System.getProperty("user.home");

        // The plugin directory
        // String a = "plugins";
        // String a = args[0];
        String a = JOptionPane.showInputDialog("The location of your workspace directory in your %HOMEPATH%:");
        System.out.println("Workspace: " + homepath + "\\" + a);

        // The artifact name
        // String b = "myCustomArtifact2";
        // String b = args[1];
        String b = JOptionPane.showInputDialog("The name of your new artifact, i.e. myCustomArtifact");
        System.out.print("Artifact Name: " + b);

        File f = new File(homepath +"\\"+ a +"\\" + b);
        if (f.exists() && f.isDirectory()) {
            System.out.println("\nA project with this artifact name already exists.");
            String z = b;
            while (b.equals(z)) {
                b = JOptionPane.showInputDialog("Please choose a unique name for your new artifact");
                System.out.println("Artifact Name: " + b);
            }
        }

        // The group name
        // String c ="org.package.name";
        // String c = args[2];
        String c = JOptionPane.showInputDialog("The name of your group, i.e. org.package.name");
        System.out.println("Group Name: " + c);

        // Check for 32 or 64 bit Windows
        String jacobDllVersionToUse;
        if (jvmBitVersion().contains("32")){
            jacobDllVersionToUse = "jacob-1.18-M2-x86.dll";
        }
        else {
            jacobDllVersionToUse = "jacob-1.18-M2-x64.dll";
        }

        File file = new File("lib", jacobDllVersionToUse);
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());

        AutoItX x = new AutoItX();

        // Open cmd
        x.run("C:\\WINDOWS\\system32\\cmd.exe", "", AutoItX.SW_SHOW);
        x.winActivate("C:\\WINDOWS\\system32\\cmd.exe");
        x.winWaitActive("C:\\WINDOWS\\system32\\cmd.exe");

        // Run archetype creation from %HOMEPATH%\\plugins
        sendCommand(x, "cd " + homepath +"\\" + a);
        /*sendCommand(x, "cd " + homepath +"\\" + "plugins");*/

        // Create the number of projects defined by the user
        sendCommand(x, "mvn archetype:generate -DarchetypeCatalog=file://"+ homepath + "/.m2/repository");
        sendCommand(x, "1");

        Thread.sleep(3000);

        UUID uuid = newUUID();

        //myGroup
        sendCommand(x, c);
        /*sendCommand(x, "myNewCustomGroup");*/

        //myArtifact
        sendCommand(x, b);
        /*sendCommand(x, "myNewCustomArtifact");*/

        //myGroup
        sendCommand(x, c);
        /*sendCommand(x, "myNewCustomGroup");*/

        //myUUID
        sendUUID(x, uuid.toString());

        //Confirm project creation
        sendCommand(x, "Y");

        //Navigate to new project directory
        /*sendCommand(x, "cd " + homepath +"\\"+ "plugins" +"\\" + "myNewCustomArtifact");*/
        sendCommand(x, "cd " + homepath +"\\"+ a +"\\" + b);

        //Confirm project creation
        System.out.println("Building project...");
        sendCommand(x, "mvn clean install");

        // Reset in preparation for next project creation
        /*sendCommand(x, "cd " + homepath +"\\" + "plugins");*/
        sendCommand(x, "cd " + homepath +"\\" + a);

        // Check if Path exists.
        /*File f = new File(homepath +"\\"+ "plugins" +"\\" + "myNewArtifact");*/
        if (f.exists() && f.isDirectory()) {
            generatePOM(a, b, c, homepath, x);
        } else {
            //The "result_message" output
            sendCommand(x, "color 0c");
            System.out.println("The project directory was not configured properly. There may be an issue with your %HOMEPATH%. Try providing the full path directly instead.");
        }

        Thread.sleep(3000);

        sendCommand(x, "color 0a");
        System.out.println("Great! Your plugin archetype is now ready for use.");

    }

    @SuppressWarnings("unused")
    private static void writeCommand(AutoItX x, String text) {
        x.send(text);
    }

    private static void generatePOM(String a, String b, String c, String homepath, AutoItX x) {
        //Navigate to project directory
        System.out.println("The project build was successfull.");
        sendCommand(x, "cd " + homepath +"\\" + a +"\\" + b + "\\" + b + "-plugin" + "\\target");

        // Extract requisite pom.xml
        System.out.println("Extracting pom.xml...");
        String zipDirectory = homepath +"\\" + a +"\\" + b + "\\" + b + "-plugin" + "\\target\\" + b + "-plugin-1.0.0.jar";
        sendCommand(x, "jar -xvf " + zipDirectory);
        sendCommand(x, "cd " + homepath +"\\" + a +"\\" + b + "\\" + b + "-plugin" + "\\target\\" + "\\META-INF\\maven\\" + c + "\\" + b + "-plugin");
        System.out.println("The directory was located successfully.");

        // Copy and move pom.xml
        String pluginDirectory = homepath +"\\" + a +"\\" + b + "\\" + b + "-plugin" + "\\target";
        sendCommand(x, "copy pom.xml " + pluginDirectory);
        sendCommand(x, "cd " + pluginDirectory);

        // Rename pom.xml
        sendCommand(x, "rename pom.xml " + b + "-plugin-1.0.0.pom");
        System.out.println("The project .pom file was generated successfully.");
    }

    // Returns if the JVM is 32 or 64 bit version
    public static String jvmBitVersion(){
        return System.getProperty("sun.arch.data.model");
    }

    private static UUID newUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid;
    }

    private static void sendUUID(AutoItX x, String uuid) {
        x.send(uuid);
        sendEnter(x);
    }

    private static void sendCommand(AutoItX x, String text) {
        x.send(text);
        sendEnter(x);
    }

    private static void sendEnter(AutoItX x) {
        x.send("{ENTER}!", false);
    }

}

为什么程序在Eclipse中执行得很好,而不是作为可运行的JAR?


共1个答案

匿名用户

假设你开始Jar

java -jar your_app.jar

那么库必须在当前目录下面的目录中

lib\

作为代码检查子目录lib中相对于当前路径的库。

假设在Jar中你有所有需要的库类(Jacob,AutoItX4Java)

.\your_app.jar
.\lib\jacob-1.18-M2-x64.dll