简体   繁体   English

从终端或命令提示符(如 git 或 maven 或 java 或 javac)运行(不是在开发期间而是在生产中)java 应用程序

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

I have created my Java CLI application (maven, executable jar).我已经创建了我的 Java CLI 应用程序(maven,可执行 jar)。 Everytime I want to run it I have to put java -jar myapp.jar.每次我想运行它时,我都必须放 java -jar myapp.jar。 How can I make it run without putting "java -jar"?如何在不放置“java -jar”的情况下运行它? The same way git runs eg git add .git运行方式相同,例如git add . or like maven eg mvn package .或者像 maven 例如mvn package I want it to run like myapp create 'file-name' where myapp is the name of my application?我希望它像myapp create 'file-name'一样运行,其中 myapp 是我的应用程序的名称? I want to open terminal and just type the name of my application and put arguments.我想打开终端并输入我的应用程序的名称并输入参数。

Are we talking windows or linux?我们说的是windows还是linux? What you typically do is create a batch or bash script to will run the java executable with the correct parameters.您通常做的是创建一个批处理或 bash 脚本,以使用正确的参数运行 java 可执行文件。 For example, on linux you can add a script in /usr/local/bin/myprogram with something like this in the contents:例如,在 linux 上,您可以在/usr/local/bin/myprogram添加一个脚本,内容如下:

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

On windows you can create a batch file.在 Windows 上,您可以创建批处理文件。 What you typically do is in the installation directory of your jar file, add a myprogram.bat with something like this in the contents:你通常做的是在你的 jar 文件的安装目录中,添加一个myprogram.bat ,在内容中添加如下内容:

@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 .然后将您的 jar 放在正确的位置,您可以使用myprogram在任何地方执行它。 Now if you add your directory to the PATH environment variable, you can execute this program anywhere from the command line.现在,如果将目录添加到 PATH 环境变量中,则可以从命令行的任何位置执行此程序。

For example, see https://www.java.com/nl/download/help/path.xml for adding things to you path.例如,请参阅https://www.java.com/nl/download/help/path.xml以将内容添加到您的路径中。

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

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