java 如何通过shell脚本执行java文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5497735/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:24:50  来源:igfitidea点击:

How to execute a java file by shell script

javashell

提问by BOSS

How to execute a java file by shell script?

如何通过shell脚本执行java文件?

采纳答案by das_weezul

If it's packed up as a jar:

如果它被打包成一个罐子:

java -jar jarfile.jar

回答by ZeWaren

回答by adarshr

java com.foo.Boo

java com.foo.Boo

回答by user11418600

All the answers only show the terminal commands to compile and run .java files but not the shell script to do so.

所有答案仅显示用于编译和运行 .java 文件的终端命令,而不显示用于执行此操作的 shell 脚本。

Below is the shell script. Let's call it compileRunJava.sh

下面是shell脚本。我们称之为 compileRunJava.sh

javac 
java ${1%.java}

You may need to giv terminal the permission to run your script -

您可能需要授予终端运行脚本的权限 -

$ sudo chmod 754 compileRunJava.sh

Let's say your .java file is Hello.java. To run the script, cd to the directory where you have Hello.java. Run the below command -

假设您的 .java 文件是 Hello.java。要运行该脚本,请 cd 到您拥有 Hello.java 的目录。运行以下命令 -

$ /path/to/shell/script/directory/compileRunJava.sh Hello.java