将 .jar 转换为 .exe

Converting .jar to .exe

我创建了一个需要将 mail.jar 添加到类路径的项目...所以我做了它,在 IDE 作品中 运行 之后的所有内容... a

在我尝试使用 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[=10=]0(GUI.java:16)
    at mail.GUI.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.access0(Unknown Source)
    at java.awt.EventQueue.run(Unknown Source)
    at java.awt.EventQueue.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.run(Unknown Source)
    at java.awt.EventQueue.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 

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

-- 请忽略项目名称 --

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

exestub.sh:

#!/bin/sh
MYSELF=`which "[=10=]" 2>/dev/null`
[ $? -gt  0 -a -f "[=10=]" ] && MYSELF="./[=10=]"
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  in
        -Xm*) JAVA_OPT="$JAVA_OPT " ;;
        -D*)  JAVA_OPT="$JAVA_OPT " ;;
        *)    PROG_OPT="$PROG_OPT " ;;
    esac
    shift
done
exec java $JAVA_OPT -jar $MYSELF $PROG_OPT

然后从您的 jar 创建 运行可用文件:

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

它的工作方式与 launch4j 的工作方式相同:因为 jar 有一个 zip format,其中 header 位于文件的 end。你可以拥有任何你想要的 header(二进制可执行文件或像这里一样的 shell 脚本)和 运行 java -jar <myexe>,因为 <myexe> 是有效的 zip/jar 文件.