eclipse 如何将 .java 或 .jar 文件转换为 Linux 可执行文件(没有 .jar 扩展名,这意味着它不是 .jar 文件)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29429976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )
提问by hiew1
I have been searching for many similar posts about this but I still can't find my answer. I want to convert a .java program into a Linux executable file, without the .jar extension. How can I do it? I am trying to use Launch4j java wrapper, JWrapper, IzPack, making a .sh, making a .bat, running it using java -jar myFile.jar etc. but none of them worked. Some procedures are complicated and difficult to debug. Is there any straightforward way to convert a .java file or .jar file into a Linux executable file?
我一直在寻找许多关于此的类似帖子,但我仍然找不到我的答案。我想将 .java 程序转换为 Linux 可执行文件,没有 .jar 扩展名。我该怎么做?我正在尝试使用 Launch4j java 包装器、JWrapper、IzPack,制作 .sh,制作 .bat,使用 java -jar myFile.jar 等运行它,但它们都不起作用。有些程序比较复杂,难以调试。有没有什么直接的方法可以将 .java 文件或 .jar 文件转换为 Linux 可执行文件?
I need to pass this program as a Linux executable as a whole into another program that takes this program as an argument.
我需要将该程序作为一个 Linux 可执行文件作为一个整体传递给另一个以该程序为参数的程序。
采纳答案by hiew1
I found a solution, which is exactly what I want after hours of searching and trying. It is to use the gcjcommand in linux.
我找到了一个解决方案,经过数小时的搜索和尝试,这正是我想要的。就是在linux下使用gcj命令。
It works like gcc for C and g++ for C++ which compile C or C++ programs into executables.
它的工作原理类似于用于 C 的 gcc 和用于 C++ 的 g++,将 C 或 C++ 程序编译为可执行文件。
First install gcj by using the terminal by typing:
首先使用终端输入以下命令安装 gcj:
sudo apt-get install gcj-jdk
After that, cd into the directory of where the .jar file is located, let's say the .jar file is myFile.jar, then do:
之后,cd进入.jar文件所在的目录,假设.jar文件是myFile.jar,然后执行:
gcj myFile.jar -o newNameofTheFile --main=theMainClassNameofTheFile
to compile the .jar file. And it should work like an executable by just running it in the command line like this:
编译 .jar 文件。它应该像一个可执行文件一样工作,只需在命令行中运行它,如下所示:
./newNameofTheFile
回答by Dyorgio
Let's say that you have a runnable jar named helloworld.jar
假设您有一个名为 helloworld.jar 的可运行 jar
Copy the Bash script below to a file named stub.sh
将下面的 Bash 脚本复制到一个名为 stub.sh 的文件中
#!/bin/sh
MYSELF=`which "cat stub.sh helloworld.jar > helloworld.run && chmod +x helloworld.run
" 2>/dev/null`
[ $? -gt 0 -a -f "$ java -jar hello.jar
hello world!
" ] && MYSELF="./$ echo "#! /usr/bin/env java -jar" > hello
$ cat hello.jar >> hello
$ chmod +x hello
"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
java_args=-Xmx1g
exec "$java" $java_args -jar $MYSELF "$@"
exit 1
Than append the jar file to the saved script and grant the execute permission to the file resulting with the following command:
然后将 jar 文件附加到保存的脚本中,并使用以下命令向该文件授予执行权限:
$ ./hello
hello world!
That's all!
就这样!
Now you can execute the app just typing helloworld.run on your shell terminal.
现在,您只需在 shell 终端上键入 helloworld.run 即可执行该应用程序。
The script is smart enough to pass any command line parameters to the Java application transparently.
该脚本足够智能,可以透明地将任何命令行参数传递给 Java 应用程序。
Credits: Paolo Di Tommaso
学分:保罗·迪托马索
Source: https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable
来源:https: //coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable
回答by Bram
What you can do is make a tiny little C-program, that calls the one of the exec functions (see man 3 exec) to the "java" binary, passing "-jar", "xxx.jar" as arguments, see also Forking a new process in C++ and executing a .jar file
你可以做的是制作一个很小的 C 程序,它调用一个 exec 函数(见 man 3 exec)到“java”二进制文件,传递“-jar”、“xxx.jar”作为参数,另见在 C++ 中创建一个新进程并执行一个 .jar 文件
回答by czheo
Another simple trick to convert a jar to a Linux executable is just putting a shebang before the jar file.
将 jar 转换为 Linux 可执行文件的另一个简单技巧是在 jar 文件之前放置一个shebang。
Say we have a hello.jar file.
假设我们有一个 hello.jar 文件。
##代码##You can create an executable in following steps.
您可以按以下步骤创建可执行文件。
##代码##Then, you will be able to execute the hello file directly.
然后,您将能够直接执行 hello 文件。
##代码##