[英]How to convert "jar" to Linux executable file?
I know how to convert "jar" to windows executable file(.exe).我知道如何将“jar”转换为 Windows 可执行文件(.exe)。 But I want to know how to convert "jar" to Linux executable file(.?).
但我想知道如何将“jar”转换为 Linux 可执行文件(.?)。 I have searched google but didn't get exact answer what i want, help to do this.
我已经搜索了谷歌,但没有得到我想要的确切答案,请帮助做到这一点。
I want to know how to convert "jar" to Linux executable file(.?).
我想知道如何将“jar”转换为 Linux 可执行文件(.?)。
Linux does not have executable files the same way that Windows does. Linux 不像 Windows 那样具有可执行文件。 In Linux we have binaries and scripts.
在 Linux 中,我们有二进制文件和脚本。 Scripts are ran with an interpreter;
脚本使用解释器运行; languages like Ruby and Python.
Ruby 和 Python 等语言。 Binaries are files of compiled code, they can be libraries or entire programs.
二进制文件是编译后的代码文件,它们可以是库或整个程序。 Both binaries and scripts can be executable.
二进制文件和脚本都可以执行。
To make a program executable in Linux, type this into the command line.要使程序在 Linux 中可执行,请将其键入命令行。
$ chmod +x myProgram
Alternatively you can open file preferences and set executable in the permissions section.或者,您可以打开文件首选项并在权限部分设置可执行文件。
Since Linux does not have .exe
files or an analogue, we'll have to work something else out.由于 Linux 没有
.exe
文件或类似文件,我们将不得不解决其他问题。 Linux and other Unix like Operating system have a shell called bash; Linux 和其他类 Unix 操作系统有一个叫做 bash 的 shell; often called the command line or terminal in reference to Linux and Mac.
通常在参考 Linux 和 Mac 时称为命令行或终端。 We want to create a file that can be run as our entire program, instead of having to call
$ java -jar myProgram.jar
.我们想创建一个可以作为整个程序运行的文件,而不必调用
$ java -jar myProgram.jar
。 To tell bash to start a script environment for a file we use a hashbang .为了告诉 bash 为文件启动脚本环境,我们使用hashbang 。 This is the first line of the file which instructs bash were to look for the interpreter to send the rest of the file to.
这是文件的第一行,它指示 bash 寻找解释器将文件的其余部分发送到。 For a bash script, like Batch Script on Windows, we would start the file with
#!/bin/bash
.对于 bash 脚本,例如 Windows 上的批处理脚本,我们将使用
#!/bin/bash
开始文件。 The path after the #!
#!
(hashbang) tells bash were to look for the interpreter. (hashbang) 告诉 bash 要寻找解释器。 For a
.jar
make the hashbang #!/usr/bin/java -jar
and then cat the .jar
to the file with the hashbang.对于
.jar
,制作 hashbang #!/usr/bin/java -jar
,然后将.jar
放入带有 hashbang 的文件中。 This can be done all from the terminal in Linux.这可以通过 Linux 的终端完成。
Create a file with the java jar hashbang.使用 java jar hashbang 创建一个文件。
$ echo '#!/usr/bin/java -jar' > myBin
We have written the hashbang as a string to the new file myBin.我们已将 hashbang 作为字符串写入新文件 myBin。
Write the jar to the file.将 jar 写入文件。
$ cat my.jar >> myBin
The >>
appends the jar to the receiving file. >>
将 jar 附加到接收文件。
This will create a file that has the bash hashbang and the jar appended to it.这将创建一个文件,其中包含 bash hashbang 和附加的 jar。 Next set
myBin
to executable and try to run the program.接下来将
myBin
设置为可执行文件并尝试运行该程序。
$ chmod +x myBin
$ ./myBin
Create a sh wrapper file with following content and make it executable:创建具有以下内容的 sh 包装文件并使其可执行:
#!/bin/bash
java -jar <your-jar>
Optionally add some vm arguments.可以选择添加一些 vm 参数。
Perhaps there are some tools that could generate such a file, but is is little effort to do it manually I reckon.也许有一些工具可以生成这样的文件,但我认为手动完成它的工作量很小。
You can always run a jar file by doing java -jar myFile.jar
.您始终可以通过执行
java -jar myFile.jar
来运行 jar 文件。
However, to make the jar file itself executable, you need to set the executable bit, as the message hints.但是,要使 jar 文件本身可执行,您需要设置可执行位,如消息提示的那样。
chmod +x /path/to/your/file/myFile.jar
will accomplish this. chmod +x /path/to/your/file/myFile.jar
将完成此操作。
After that you can do ./myFile.jar
to run it.之后,您可以执行
./myFile.jar
来运行它。
man chmod
will provide you with information about how chmod works. man chmod
将为您提供有关 chmod 如何工作的信息。
Source: How can I make a .jar file executable?资料来源: 如何使 .jar 文件可执行? on AskUbuntu.
在 AskUbuntu 上。 Answer by Gary
加里的回答
While making a shell script works, it sort of seems a bit like overkill for most situations.虽然使 shell 脚本有效,但在大多数情况下似乎有点矫枉过正。 Why not just use an alias and throw it in your
.bashrc
or .bash_aliases
file?为什么不直接使用别名并将其放入
.bashrc
或.bash_aliases
文件中? Like so:像这样:
alias whatever='java -jar /path/to/whatever.jar'
(Since my solution didn't come up as an answer here, my "why" is a somewhat genuine question: Is there some hidden hazard or feature I'm missing? Or is it more of a matter of adhering to the strictest interpretation of the OP's question?) (由于我的解决方案没有在这里作为答案,我的“为什么”是一个有点真实的问题:是否有一些隐藏的危险或我缺少的功能?或者更多的是坚持最严格的解释OP的问题?)
在这种情况下,可能更恰当和准确的是创建一个桌面配置文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.