doukong5394 2014-01-28 06:49
浏览 68

如何使用Java创建命令行应用程序? [重复]

This question already has an answer here:

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?

</div>
  • 写回答

3条回答 默认 最新

  • dou9022 2014-01-28 06:56
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器