如何将包装shell脚本中的参数传递给Java应用程序? [英] How to pass arguments from wrapper shell script to Java application?

查看:135
本文介绍了如何将包装shell脚本中的参数传递给Java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在命令行(linux和mac)上运行我正在创建的Java程序。我不想一直输入java和参数,所以我正在考虑创建包装器脚本。有什么最好的方法可以让它们在任何地方工作?我也希望能够传递参数。我正在考虑使用shift来执行此操作(删除第一个参数)。

I want to run Java programs I am creating at on the command line (linux and mac). I don't want to type "java" and arguments all the time, so I am thinking about creating wrapper scripts. What's the best way to do this so that they work everywhere? I want to be able to pass arguments, too. I was thinking of using "shift" to do this (removing first argument).

有没有更好的方法可以在不使用脚本的情况下执行此操作?也许制作一个不需要通过java命令调用的可执行文件?

Is there a better way to do this without using scripts at all? Perhaps make an executable that doesn't require invocation through the "java" command?

推荐答案

假设您使用的是shell与Bourne shell兼容;例如sh,bash,ksh等,以下包装器将所有命令行参数传递给java命令:

Assuming that you are using a shell that is compatible with the Bourne shell; e.g. sh, bash, ksh, etc, the following wrapper will pass all command line arguments to the java command:

#!/bin/sh
OPTS=... 
java $OPTS com.example.YourApp "$@"

$ @ 扩展为shell脚本的其余参数,并在其周围加上引号会导致参数被单独引用,以便以下内容工作:

The $@ expands to the remaining arguments for the shell script, and putting quotes around it causes the arguments to be individually quoted, so that the following will work:

$ wrapper "/home/person/Stupid Directory/foo.txt" 

这篇关于如何将包装shell脚本中的参数传递给Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆