How can I create a command line application in Java? [duplicate] - java

This question already has answers here:
How to create a Java application which can be run by a click?
(8 answers)
Closed 9 years ago.
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?

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:
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.
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

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.

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

Related

Java calling an external process - exe

I am building a java application, which at some point utilizes an external exe. At this point, I'm trying to simply add this exe as some sort of library, which I can use in process call, so user wouldn't have to install it..
This exe file is an command line tool which produces some output, which is further processsed by the application.
So my question is, how does one include exe file within a java application, instead of calling it as a system process. Also acceptable would be, if this exe would be for example in the final lib folder, where java app would fetch it and execute it.
I hope it is clear and thanks for any help.
Java interacts with other (native) code in a couple of ways:
to specifically coded libraries through Java Native Interface (JNI)
to external tools by forking a child process using Runtime.getRuntime().exec(...)
through network communications
In the first case, it is useful to include your JNI library with you Java program. It's thinkable to include it as a class resource in your JAR file, but that would be against the ideas of JAR files (holding classes and resources). More likely you should bundle the library (for all target platforms) with some sort of installation package (e.g. InstallAnywhere or others)
In the second case it's not useful to include the binary into your Java program (i.e. in the JAR file). Rather it most likely has (should have) its own installation procedure and your Java code should use an appropriate commandline (PATH) to find the executable.
I think the third case is not relevant.

How to turn a JAR file to an EXE file manually? [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 9 years ago.
Well I have made a few games and programs in java, and I was wondering, HOW DO YOU MAKE THIS EXE? I have been looking around google for the answer and I couldn't find any program or website that helped. So I want to make it EXE myself, manually. I have already tried JSmooth, and that didn't work.
You need to learn about creating an executable jar. Once you create that, you can simply run it by double clicking on that. Here is a tutorial to help you with that:
http://www.mkyong.com/java/how-to-make-an-executable-jar-file/
If you want to create a batch/cmd/sh script then you can put your java command in that script to invoke your main class.
A sample batch script taken from a related post(How to run java application by .bat file):
#ECHO OFF
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;path/to/needed/jars/my.jar
%JAVA_HOME%\bin\java ro.my.class.MyClass
On the same lines, you can write a shell script on linux/unix.
Java now ships with a tool that can convert your application into an executable. The documentation is written for JavaFX applications. I think you should be able to use it for any java application though.
Check out this article discussing how to deploy a JavaFX Application:
http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm
Using this tool requires that you install Inno5 Setup (for creating an exe) or WiX (for creating an msi) or both if you want to create and deploy both an exe and an msi.
If this tool that ships with Java doesn't work for you there are projects such as install4j that convert your project to an exe. See http://www.ej-technologies.com/products/install4j/overview.html.

Converting dll to jar

I'm trying to find a way to convert a dll to a jar file. I have a .net application that communicates with a java application. The core entities are .net objects which I have to duplicate manually in java.
I've read about IKVM but it seems that it converts only jars to dlls and not the other way around.
Edit: If there is a tool that creates java classes from a dll it is also fine.
Thanks in advance
There isn't such a tool.
A dll is a natively compiled library. That means its been compiled down to machine code. Probably compiled by a C/C++/C# compiler.
A jar file is a zip file that contains '.class' files, which are files compiled down to 'java virtual machine code'. Probably compiled by a java/clojure/scala compiler.
These are two very different incompatible things.
It's not impossible to create such a tool that would do this translation, but it would definitely be an extremely difficult task, as it would entail translating from one machine code, to another, and would need to manage multiple issues like dependency solving, different type structure etc.
HOWEVER, I'm imagining that you want to do this because you want to use a DLL within some java code. That is somewhat possible, but is actually quite complicated. You'll need to use the JNI.
Take a look at this question as it might help you achieve what you want to do:
Calling C++ dll from Java
This is actually an easy task to perform. Converting .dll to .jar is as simple as using com4j and a couple of commands on the command line.
Download com4j.
Open command line and navigate to com4j directory in above step.
Execute below command.
java -jar tlbimp.jar -o outputFolder -p nameOfPackage "pathToFile"
Then jar the results with the following:
jar cf desiredJarName.jar folderYouWantJard

Run jar in a java apllication ( pdfbox )

I'm programming an application in java that stores data and images to a pdf. I'm using pdfbox. I saw that pdf box has some jar like the one for data encryption that has to be run by command lines.
Is there a way to run those jar in one of the functions of my class? I mean start the encryption processed by this jar after an event. How do i launch command lines from within my class?
For example:
if(IHaveData() && FileCreated())
*Encrypt the data*
Thanks in advance to everyone who wants to help even if the question may be stupid or something
We do not run jars. We run applications. Java applications consist of a collection of classes and resources that can be stored either in file system or in jar files.
To run java application you should use command line like
java -cp one.jar;two.jar;three.jar Main
where
x.jar - the jar files
; - separator for windows. Use : for unix
Main is a fully qualified class name of your entry point.
You obviously can run any command line from java program using Runtime.getRuntime().exec() or using ProcessBuilder. "Any" means that you obviously can run java application from other java application.
However this way has a lot of obvious disadvantages. Instead you should run API that is exposed by library you are using. If it does not expose convenient API you can in worse case directly call the main method from your program:
Main.main(new String[] {param1, param2})

how can I create executable file for the program written on Java?

everyone, how can I create executable file for the program written on Java in Eclipse Helios? I mean to create small icon to be able start program only by double-clicking on its icon, thanks in advance
edited
I mean executable for Windows
Export .jar in eclipse. (how to)
Use JSmooth (info) to make an .exe file. (how to)
Here is a tutorial that shows you how to make a jar file from eclipse.
If Java is installed on the computer, you can execute your application by doubleclicking the jar file:
http://ecs.victoria.ac.nz/Courses/COMP205_2009T1/TutorialEclipseExportToJar
You didn't mention what platform you are using. There are 2 ways I can think of.
The easiest way is for you to create a *.bat file (in Windows) that contains the java YourApp command line.
If you want to create a more fancy installer and executable, you can use NSIS script to do so. Since you are using eclipse, consider trying EclipseNSIS to generate the NSIS script, which is much faster and easier than writing it yourself from ground up.
The best answer for this situation is to launch the app. using Java Web Start. JWS can not only create desktop and start menu launch items, but provides automatic updates, cross-platform compatibility and much more.
Create a 1-line metafile to specify which class the JVM should look for to start with the main(String[]) method.
Run the command jar cmf [metafileName] [jarfileName] [classfiles] [img/txtDirectories]
You have an executable jar file - type in "java -jar jarfileName" or, directly "jarfileName" at your prompt. On windows, you can also double click on the jar file logo/name to get it started.
Good wishes, - M.S.
PS: Here is the link to a more detailed tutorial:
http://csdl.ics.hawaii.edu/~johnson/613f99/modules/04/jar-files.html

Categories

Resources