Running (not during development but in production) java application from terminal or command prompt like git or maven or java or javac

huangapple go评论94阅读模式
英文:

Running (not during development but in production) java application from terminal or command prompt like git or maven or java or javac

问题

我已经创建了我的Java命令行应用程序(使用Maven构建的可执行jar)。每次我想要运行它时,我都必须输入java -jar myapp.jar。我该如何使它在不输入“java -jar”的情况下运行呢?就像git命令运行git add .,或者像Maven一样运行mvn package。我希望它可以像这样运行:myapp create 'file-name',其中myapp是我的应用程序的名称。我想要在终端中只输入应用程序的名称并添加参数即可。

英文:

I have created my Java CLI application (maven, executable jar). Everytime I want to run it I have to put java -jar myapp.jar. How can I make it run without putting "java -jar"? The same way git runs e.g. git add . or like maven e.g. mvn package. I want it to run like myapp create 'file-name' where myapp is the name of my application? I want to open terminal and just type the name of my application and put arguments.

答案1

得分: 1

我们是在讨论 Windows 还是 Linux?通常您需要创建一个批处理或者 Bash 脚本来运行带有正确参数的 Java 可执行文件。例如,在 Linux 上,您可以在 /usr/local/bin/myprogram 添加一个脚本,内容类似于:

#!/bin/bash
/usr/bin/java [options] -jar /usr/local/libs/myprogram.jar $*

在 Windows 上,您可以创建一个批处理文件。通常情况下,在您的 jar 文件的安装目录中,添加一个 myprogram.bat 文件,内容类似于:

@ECHO OFF
%JAVA_HOME%\bin\java [options] -jar myprogram.jar %*

然后将您的 jar 文件放在正确的位置,您就可以使用 myprogram 在任何地方执行它。如果您将目录添加到 PATH 环境变量中,您就可以在命令行的任何地方执行此程序。

例如,参考 https://www.java.com/nl/download/help/path.xml 了解如何将内容添加到您的路径中。

英文:

Are we talking windows or linux? What you typically do is create a batch or bash script to will run the java executable with the correct parameters. For example, on linux you can add a script in /usr/local/bin/myprogram with something like this in the contents:

!/bin/bash
/usr/bin/java [options] -jar /usr/local/libs/myprogram.jar $*

On windows you can create a batch file. What you typically do is in the installation directory of your jar file, add a myprogram.bat with something like this in the contents:

@ECHO OFF
%JAVA_HOME%\bin\java [options] -jar myprogram.jar %*

Then put your jar in the right place and you can execute it anywhere with myprogram. Now if you add your directory to the PATH environment variable, you can execute this program anywhere from the command line.

For example, see https://www.java.com/nl/download/help/path.xml for adding things to you path.

huangapple
  • 本文由 发表于 2020年8月24日 15:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定