简体   繁体   English

如何制作可运行的jar文件?

[英]How to make a runnable jar file?

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well.我曾经使用过一个程序来制作可运行的 .jar 文件。我现在找到的所有文件都是制作 .exe 文件的。我记得它也可以选择将文件制作为 .sh 脚本。 Anyone knows its name?有人知道它的名字吗? I've been searching for hours with no avail :/我已经搜索了几个小时但无济于事:/

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well.我曾经使用过一个程序,该程序可以生成可运行的.jar文件。我现在发现的所有程序都可以生成.exe文件。 Anyone knows its name?有人知道它的名字吗? I've been searching for hours with no avail :/我一直在搜索无济于事的小时:/

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well.我曾经使用过一个程序,该程序可以生成可运行的.jar文件。我现在发现的所有程序都可以生成.exe文件。 Anyone knows its name?有人知道它的名字吗? I've been searching for hours with no avail :/我一直在搜索无济于事的小时:/

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well.我曾经使用过一个程序,该程序可以生成可运行的.jar文件。我现在发现的所有程序都可以生成.exe文件。 Anyone knows its name?有人知道它的名字吗? I've been searching for hours with no avail :/我一直在搜索无济于事的小时:/

If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar ), here is a post explaining the process: how to create executable jars .如果您想要一个可以使用通常的语法./app.jar (而不是java -jar )执行的 jar,这里有一篇解释该过程的帖子: 如何创建可执行的 jars

Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it.基本上,JAR 是 ZIP 的一种变体,它允许在不破坏 JAR 的情况下将随机字节预先/附加到 JAR。 This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".这意味着可以在 jar 的开头添加启动程序脚本以使其“可执行”。

Here is a simple example of the process:这是该过程的一个简单示例:

# Append a basic launcher script to the jar
cat \
  <(echo '#!/bin/sh')\
  <(echo 'exec java -jar $0 "$@"')\
  <(echo 'exit 0')\
  original.jar > executable.jar

# Make the new jar executable
chmod +x executable.jar

With this, you can now run ./executable.jar instead of java -jar original.jar .有了这个,您现在可以运行./executable.jar而不是java -jar original.jar This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.这适用于所有类Unix 系统,包括 Linux、MacOS、Cygwin 和 Windows Linux 子系统。

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

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