How can I create a command line application in Java?

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

How can I create a command line application in Java?

问题

我正在编写一个命令行工具,希望它可以在所有流行的平台上运行(Windows、Mac和Linux)。我考虑使用golang来编写,因为它可以生成静态编译的二进制文件,并且可以在上述所有平台上运行。在做出决定之前,我需要评估一下Java,看看它是否能够实现这个功能。

我想知道在Java中是否可以创建一流的命令行程序?据我所知,Java程序需要像这样启动:

java -cp classpath MyApp

现在我需要将其包装在一个shell脚本中,以改善用户体验。类似于:

#!/bin/sh
java -cp classpath MyApp $@

现在用户可以这样做:

myapp --arg1 value --arg2 value

问题是这种方法不跨平台。在Windows上,我需要编写一个批处理文件来实现这一点。

我不确定这是否是在Java中处理这个问题的正确方式。是否有更好的方法来解决这个问题?

英文:

I am in the process of writing a command line tool which should work on all popular platforms (Windows, Mac & Linux). I was thinking of writing it in golang because it can generate statically compiled binaries and works on all the above said platforms. Before I take the decision, I need to evaluate Java and see if it can do this.

I'm wondering is it possible to create first class command line programs in Java? AFAIK, a Java program needs to be started like

java -cp classpath MyApp

Now I need to wrap this in a shell script to improve the experience. Something like,

#!/bin/sh
java -cp classpath MyApp $@

Now user can do:

myapp --arg1 value --arg2 value

The problem is this approach is not cross-platform. On Windows, I need to write a batch file to do this.

I'm not sure if this is the right way to do in Java. Is there a better way to handle this problem?

答案1

得分: 2

你发布的解决方案(调用Java的特定平台脚本文件)确实是最简单和最快实施的方法。如果这对你的需求还不够,有几种解决方案可以提供更好的可用性:

  1. 可执行的JAR文件。通过在MANIFEST.MF条目中指定主类,该文件可以通过在某些平台上双击来执行(在Windows上肯定可以,在其他平台上我不确定)。请注意,这需要在计算机上正确安装了JRE。

  2. 服务包装器。有第三方产品可以将你的Java应用程序封装为本机可执行文件,并使你的应用程序“感觉”像本机应用程序一样。最常用的是tanuki服务包装器,但还有其他选择(launch4j是一个开源替代方案,适用于你所需的操作系统集合)

编辑 - 在相关的SO问题这里中有更多选项

英文:

the solution you posted (platform specific script files invoking java) is indeed the easiest and quickest to implement. if thats not enough for your needs there are several solutions which provide better usability:

  1. executable jar files. basically by stating your main class in a MANIFEST.MF entry the file becomes executable by double clicking on some platforms (windows definitely, the rest im not certain). note that this requires a properly installed JRE on the machine.
  2. service wrappers. there are 3rd party products that will wrap your java application in a native executable and allow your application to "feel" like a native app. the most commonly used one is the tanuki service wrapper but there are others (launch4j is an open source alternative that fits your set of required operating systems)

EDIT - more options in a related SO question here

答案2

得分: 1

为了方便处理命令行参数,你可以使用Apache CLI。它会解析参数,并在缺少参数时打印出它们的列表。在Java项目中,将*.bat*.sh文件都放在bin文件夹中是完全可以的。这种方法有什么让你困惑的地方吗?你可以参考Ant或Maven项目的做法。

英文:

To work with command line arguments comfortably, you can use Apache CLI. It parses arguments and print a list of them if some are missing. In java projects it's always OK to have *.bat and *.sh files both in the bin folder. What confuses you in this approach? Have a look at Ant or Maven projects for example.

答案3

得分: 0

我建议使用Maven,它有一个叫做"maven spring shell"的概念,非常容易开发。你确实需要为Windows和Linux编写.bat和.sh文件,但这更多是一种配置类型。打包会更容易。以下链接可以帮助你开发Spring Shell应用程序:开发Spring Shell应用程序

英文:

I would probably suggest to use Maven, It has a concept called "maven spring shell" which would be easy to develop. You indeed need to write .bat and .sh for windows and linux, but it is more of a configuration type. Would be easier to package.
The following link would help you developing spring shell applications

huangapple
  • 本文由 发表于 2014年1月28日 14:49:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/21398755.html
匿名

发表评论

匿名网友

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

确定