How can an executable .jar file be run without the command prompt? - java

I have a very basic .jar file that successfully runs, though I can only seem to run it by doing one of two things:
Using the command prompt and entering a command such as java -jar test.jar
Creating a shortcut with the path being java -jar C:\Users\Nick\Documents\test.jar
Is there a way to run a .jar file without having to do either of these two things, IE a way to run it from within Windows Explorer?
Edit:
My .jar file looks like this:
Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: base.MainClass
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
I used Eclipse to create this. Should I edit the Main-Class to just be base.MainClass, and remove anything with Rsrc in it?

Double click on it
use something to wrap the JAR file into a common exe (e.g. http://launch4j.sourceforge.net/)
Note: When you double-click on a JAR and the JAR just prints out something on the console, the window could close immediately after the execution. In this case you won't see much. But your program was executed correctly and just the window was just closed after the execution. Try to open a JFrame in your application, then you should see the frame when you double-click on the JAR.

Make a .bat file in the distribution directory.
#echo off
start javaw -Dfile.encoding=UTF8 -jar test.jar
exit
This .bat file will also fix problems with special characters working in eg. netbeans, but not when double clicking the .jar
If you don't need support for special characters you can leave out the "-Dfile.encoding=UTF8" part.

My JARs are associated with 7Zip. To run an executable JAR, right click the file name and select Open With > Java.

Installing Java should create a shortcut for your OS, so that you can open executable jars by double click.
If it doesn't work for you, you have to investigate how to do it for your version of the OS.
I guess for Windows it is right-clicking on the app, and then configuring the "open with ..." dialog.
The program to run is afaik:
javaw -jar "%*"
where you have to specify the whole path to javaw, if it isn't in the PATH.
If the Java program expects command line arguments itself, for example a program to rotate an image might expect image files as arguments, so you can draw them with the mouse on the jarfile, therefore you specify the windows syntax for "all parameters" which is "%*" or something similar. The manual of your OS should answer the question.

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.

Running a JAR from the windows contextual menu

I've got a java application packaged as jar, which I am trying to add as a "right click" option for a specific file type in windows.
I've added the command reg key to the filetype, and the command appears in windows explorer when I rightclick the appropriate file type, but the jar doesn't run successfully.
The value of the (default) command is:
java.exe "c:\MYAPPDIR\MYAPP.jar %1"
and a command prompt pops up quickly and closes too fast for me to see what is going on.
This exact command works mind in a cmd.exe prompt (where %1 is replaced with a valid file name)
I tried java instead of java.exe, and variations on where the quotations go, but no success.
How do I format the command? Any help is appreciated!
You need to create a Runnable Jar File in order for it to run when you click on it. You can run any jar file by doing java -jar File.jar. You can create a runnable jar file in Eclipse, or you can write the MANIFEST text files by yourself.

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.

System.out not working when calling jar-file from Windows command line

I have this class:
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
And I used Eclipse's "Export to runnable JAR" function to make it into a executable jar-file (test.jar). When I run this program in Eclipse, it prints out "Hello World!", but when I call the jar-file from the command line, nothing happens.
I have tried calling the jar-file like this:
test.jar
test.jar -jar
There is no output whatsoever.
I have another program that also has a side effect (in addition to output to stdout) and while the side effect is being performed (which tells me the jar-file was definitely executed), again no output is given to the command line. I have also tried using stderr, but that makes no difference. Does anybody know how I can make this work?
Thanks!
You must run the JAR using
java -jar test.jar
(Your JRE's bin folder must be added to PATH in order to get the command working from any location)
NOTE: I know you created the JAR using Eclipse but you might want to know how does an executable JAR works
The previous answers are correct, so I'll just clarify a bit the "executable jar" concept.
There's no such thing as an "executable jar", equivalent to an exe file in windows, in an "executable jar" you'll be only specifying the "entry" point ( your main class you want to be executed by default )
A jar is basically just an archive, you'll still need java to actually launch the jar ( java -jar your.jar )
The fact that in windows you might have an association with javaw.exe means this will be launched by the OS when you double-click on the jar, like a .txt file being opened automatically with notepad, it doesn't make the .txt an executable file.
Check this out as well :
JAR files revealed
When you invoke the jar using 'test.jar' the starting of the app is handed off to the registered java handler for jar files, which doesn't run in the context of the command line.
The default jar handler doesn't open console based System.{out,err} file handles, as it would mean a cmd style window for each of the jar files launched, which is not an ideal situation.
The previous answer, using java -jar test.jar causes it to run within the context of the current cmd window, and thus you will see the output.

How do I create executable Java program? [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 8 years ago.
I have programmed a Java Program in JCreator, everything is done, but I want to create an executable file from it, ie I dont want to have to run the program by loading the java classes and compiling then executing, but instead have it as a stand alone executable file.
What the quickest way to do this?
You can use the jar tool bundled with the SDK and create an executable version of the program.
This is how it's done.
I'm posting the results from my command prompt because it's easier, but the same should apply when using JCreator.
First create your program:
$cat HelloWorldSwing.java
package start;
import javax.swing.*;
public class HelloWorldSwing {
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
class Dummy {
// just to have another thing to pack in the jar
}
Very simple, just displays a window with "Hello World"
Then compile it:
$javac -d . HelloWorldSwing.java
Two files were created inside the "start" folder Dummy.class and HelloWorldSwing.class.
$ls start/
Dummy.class HelloWorldSwing.class
Next step, create the jar file. Each jar file have a manifest file, where attributes related to the executable file are.
This is the content of my manifest file.
$cat manifest.mf
Main-class: start.HelloWorldSwing
Just describe what the main class is ( the one with the public static void main method )
Once the manifest is ready, the jar executable is invoked.
It has many options, here I'm using -c -m -f ( -c to create jar, -m to specify the manifest file , -f = the file should be named.. ) and the folder I want to jar.
$jar -cmf manifest.mf hello.jar start
This creates the .jar file on the system
You can later just double click on that file and it will run as expected.
To create the .jar file in JCreator you just have to use "Tools" menu, create jar, but I'm not sure how the manifest goes there.
Here's a video I've found about: Create a Jar File in Jcreator.
I think you may proceed with the other links posted in this thread once you're familiar with this ".jar" approach.
You can also use jnlp ( Java Network Launcher Protocol ) too.
If you are using Eclipse , you can try the below 7 steps to get a .exe file for Windows.
Now you have a JAR file. Use java -jar path/jarname.jar to execute.
If you want to convert this to .exe, you can try http://sourceforge.net/projects/launch4j/files/launch4j-3/
STEP7:
Give the .xml file an appropriate name and click "Save". The .xml file is standard, don't worry about it. Your executable file will now be created!
I'm not quite sure what you mean.
But I assume you mean either 1 of 2 things.
You want to create an executable .jar file
Eclipse can do this really easily File --> Export and create a jar and select the appropriate Main-Class and it'll generate the .jar for you. In windows you may have to associate .jar with the java runtime. aka Hold shift down, Right Click "open with" browse to your jvm and associate it with javaw.exe
create an actual .exe file then you need to use an extra library like
http://jsmooth.sourceforge.net/ or http://launch4j.sourceforge.net/ will create a native .exe stub with a nice icon that will essentially bootstrap your app. They even figure out if your customer hasn't got a JVM installed and prompt you to get one.
On the command line, navigate to the root directory of the Java files you wish to make executable.
Use this command:
jar -cvf [name of jar file] [name of directory with Java files]
This will create a directory called META-INF in the jar archive. In this META-INF there is a file called MANIFEST.MF, open this file in a text editor and add the following line:
Main-Class: [fully qualified name of your main class]
then use this command:
java -jar [name of jar file]
and your program will run :)
Take a look at WinRun4J. It's windows only but that's because unix has executable scripts that look (to the user) like bins. You can also easily modify WinRun4J to compile on unix.
It does require a config file, but again, recompile it with hard-coded options and it works like a config-less exe.
Jexecutable can create Windows exe for Java programs. It embeds the jars into exe file and you can run it like a Windows program.
You could use GCJ to compile your Java program into native code.
At some time they even compiled Eclipse into a native version.
Take a look at launch4j
Write a script and make it executable. The script should look like what you'd normally use at the command line:
java YourClass
This assumes you've already compiled your .java files and that the java can find your .class files. If java cannot find your .class files, you may want to look at using the -classpath option or setting your CLASSPATH environment variable.
Java Web Start is a good technology for installing Java rich clients off the internet direct to the end user's desktop (whether the OS is Windows, Mac or *nix). It comes complete with desktop integration and automatic updates, among many other goodies.
For more information on JWS, see the JWS info page.
As suggested earlier too, you can look at launch4j to create the executable for your JAR file. Also, there is something called "JExePack" that can put an .exe wrapper around your jar file so that you can redistribute it (note: the client would anyways need a JRE to run the program on his pc)
Exes created with GCJ will not have this dependency but the process is a little more involved.

Categories

Resources