首页 > 解决方案 > 将 .jar 转换为 .exe

问题描述

我创建了一个需要将 mail.jar 添加到 Classpath 的项目......所以我做到了,并且在 IDE 中运行后的所有内容都可以工作......

在我尝试使用 launch4j 编译带有自定义类路径的 .exe 文件后,我得到了以下输出:

    Executing: C:\Users\resti\Desktop\tic-tac-toe.exe
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/mail/Authenticator
    at mail.FileListing.Listuj(FileListing.java:54)
    at mail.GUI.jButton1ActionPerformed(GUI.java:147)
    at mail.GUI.access$000(GUI.java:16)
    at mail.GUI$1.actionPerformed(GUI.java:51)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
    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)
    ... 40 more 

所以我推断在构建过程中添加类路径时出现错误,尽管设置了类路径,但有人不应该提示如何修复它

-- PLS 忽略项目名称 --

这是自定义类路径截图

标签: javacompilationlaunch4j

解决方案


Launch4j适用于 Windows 和 Linux/Mac。但是,如果您运行的是 Linux/Mac,有一种方法可以将您的 jar 嵌入到为您执行自动启动的 shell 脚本中,因此您只有一个可运行文件:

exestub.sh:

#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt  0 -a -f "$0" ] && MYSELF="./$0"
JAVA_OPT=""
PROG_OPT=""
# Parse options to determine which ones are for Java and which ones are for the Program
while [ $# -gt 0 ] ; do
    case $1 in
        -Xm*) JAVA_OPT="$JAVA_OPT $1" ;;
        -D*)  JAVA_OPT="$JAVA_OPT $1" ;;
        *)    PROG_OPT="$PROG_OPT $1" ;;
    esac
    shift
done
exec java $JAVA_OPT -jar $MYSELF $PROG_OPT

然后你从你的 jar 中创建你的可运行文件:

$ cat exestub.sh myrunnablejar.jar > myrunnable
$ chmod +x myrunnable

它的工作方式与 launch4j 的工作方式相同:因为 jar 具有zip 格式,其标头位于文件末尾。你可以有任何你想要的头文件(二进制可执行文件或者像这里的 shell 脚本)并运行java -jar <myexe>,因为<myexe>它是一个有效的 zip/jar 文件。


推荐阅读