1

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?

Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
  • related: http://stackoverflow.com/questions/2288440/how-to-create-a-java-application-which-can-be-run-by-a-click – jacquard Jan 28 '14 at 06:53
  • http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file – Mikhail Jan 28 '14 at 07:31

3 Answers3

2

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

Community
  • 1
  • 1
radai
  • 23,949
  • 10
  • 71
  • 115
1

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.

Mikhail
  • 4,175
  • 15
  • 31
0

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

Tanveer Ali
  • 393
  • 3
  • 6