Double click .jar file to launch it in command line - java

I'm trying to make one of my command line utilities a little more user friendly. Most of my co-workers don't mind using the utility as a CLI, but navigating to it is a bit of a pain (to them). I'd rather not go to every computer and set up a shortcut in their CLI so:
Is there a way to make a .jar file launch a command line utility into a command prompt (preferably PowerShell?) I tried searching Google and Stack Overflow but am having a hard time making headway... Any direction would be much appreciated.
I somehow imagine this using Desktop, but am not sure how that would work.

Maybe you need to make a swing based console to redirect output and input. Here are the links I found in a simple web search. (I've never used these before)
Message Console
Simple Java Console
And an open source project here at Swing-Console.
EDIT:
Another option. What if you distribute your application with a run-me.bat file?
#echo off
java -jar my-console-app.jar
You can also change the title.

You should reassociate jar files with java.exe instead of javaw.exe. javaw is the non-CLI version of Java, but launching it with java.exe instead should do the trick.
This is how to do that(provided you have administrative rights):
assoc .jar=jarfileterm
ftype jarfileterm="PATH\TO\JRE\bin\java.exe" -jar "%1" %*
Of course, replace PATH\TO\JRE with the correct path.
After doing this, double-clicking a .jar file should open a CLI.

You have at least two options.
Make an executable jar file.
An example on Windows would look something like this:
mkdir temp
cd temp
for %f in (..\dist\lib\*.jar) do #jar xf %f
jar xf ..\dist\YourJar.jar
jar cfe YourJar.jar com.something.Main *.*
You can read more about the jar archive tool here: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
This is a lot easier with Maven. See the Maven shade plug-in here: https://maven.apache.org/plugins/maven-shade-plugin/
Make a Java Web Start application
For an overview, take a look at this:
http://docs.oracle.com/javase/tutorial/deployment/webstart/
For a step by step guide, take a look at this: http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/developersguide/contents.html

Related

How to launch a cli Jar with double click?

Is it possible to launch a cli java project by double click on the jar ?
Inside the jar, we have the manifest file with the main class well defined, but when we try to double click on jar it can't launch it and displays a generic error : The java jar file could not be launched.
We supposed that it's because it is only able to run this jar from the cli.
Is it right ?
PS : Sorry for my bad english, I'm french :)
Thanks !
Unless the jar-file includes some sort of implementation of Runtime that runs the system's terminal or command prompt, it won't open a terminal/prompt window when double clicking it (if some sort of GUI-implementation have been made with e.g. Swing, it will however launch the GUI). However, you can create a separate file, which will launch the jar-file.
As it seems you're on Mac, you can just create a .command-file. If you just need to execute your .jar-file, create a file with the following content:
#! /bin/bash
java -jar /path/to/file.jar
Name it something you remember, but don't forget to add .command at the end.
For Linux, use .sh extension, with the same content.
For Mac and Linux, you might have issues with executing the files because of lacking the permissions, see here for changing permissions on files.
For windows, use .bat extension. Exchange the slashes with backslashes when defining the path, and omit the #! /bin/bash-line. You'll also have to add Java to your environment variables, see here.

Unable to run jar file in Windows by double clicking

I am trying to run a very simple program by double clicking my Jar file with javaw.
It runs fine from Netbeans AND from the command line.
My registry path to javaw:
"C:\Program Files\Java\jre1.8.0_102\bin\javaw.exe" -jar "%1" %*
What is going wrong here? How can I make this work when I double click the Jar?
Make sure your JAR file is an executable JAR; I'm not entirely sure how to export an executable JAR from NetBeans (I use Eclipse), but this article seems to explain it pretty well.
Make sure javaw.exe is the default program for running JARs. This question addresses that.
There's also a Java Tutorial on making and using JARs, and several other StackOverflow questions about it.

Shortcut to execute .jar in command line (terminal)

I have written a program in Java that one can use via the Terminal CLI. I've packaged it in a .jar, and it works nicely, however it happens to be a tool that I'd like to use quite often via CLI.
I am familiar with executing .jar files using:
java -jar MyJarFile.jar
but I'd like to be able to run this using a single command.
The only way I've done something similar to this is by using an NSTask object in Obj-C (my skills in C/C++ are limited), but this is a slightly different situation.
The solution might be staring me in the face, and might be quite simple, but any help to find a different method for launching my .jar would be greatly appreciated.
Thanks
Shell script would work too.
#!/bin/bash
java -jar MyJarFile.jar
save this as RunMyJarFile.sh and give it execute permissions.
./RunMyJarFile.sh
is all you need to do.
Just add an alias to your .bashrc like this:
echo "alias mycmd=\"java -jar MyJarFile.jar\"" >> ~/.bashrc
then source it (or reopen the terminal) with:
source ~/.bashrc
now you can just type mycmd.
you can use Launch4J or other java wrappers.
You've got a couple of choices:
Just click on the Jar from the GUI - on most (all?) platforms, double-clicking on executable Jars will run them. If that works for your use case, that's the easiest thing to do.
Just put your java -jar ... call into a shell script; put it on your PATH if you want. Quick and easy, but it requires keeping track of both the shell script and the Jar file, which might end up being a case of "now you have two problems".
Use a wrapper utility to convert your Jar into a binary or .exe installer. These will bundle your Jar with a script or binary that will unpack and run your Jar automatically for you. It's been a couple years, but I've used NSIS to create an .exe installer pretty painlessly.

Java create executable cmd line program(windows)

I'm new to java and have recently created a stress testing application to test server configurations. Its very simple and all is done within cmd line. I used eclipse to create the jar file and that seems to have worked fine.
The problem that I am running into is making this executable. If I use java -jar in windows cmd line to execute the program, it runs fine. However, I need to be able to run it by "double clicking" the jar file(right now I click on it and nothing happens) or create a .exe which defeats the purpose of java, but this will only be used in windows.
When I click on the jar now nothing happens, but when using the java - jar in cmd it works. Not all of the computers have java in the cmd line, but have it installed. I'm not sure why a cmd window doesnt pop when clicking on the jar?
Again I'm new and any help is much appreciated!!
Create a sortcut icon that will do java -jar yourFile.jar
In windows, you can associate the jar file with the JRE jar runner. Take a look at this post, which explains your options pretty well.
Make a bat file for Windows. You can do this by the following:
#echo off
java -jar YourJarName.jar
Save this in a text file with the .bat extension.
It should run the JAR once double clicked if the JAR file is in the same directory as the .bat file. Otherwise you will have to navigate to the JAR file relative to where the .bat file is located.
You said you didn't want an exe but not sure if this will be ok for you. It shouldn't be a problem for someone to click the .bat file first and will work in all cases under Windows.
Hope this helps.
If you want to get really awesome with it and have it show up in your Task Manager with an app.exe naming and handle any startup options, you should read into JNI. JNI will allow you to wrap the starting and stopping of a Java app using a windows executable and it is actually very simple to implement.
If you want something as simple as a windows exe launcher, there are also tools out there such as Launch4j will create exe wrappers for you.

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