Running a JAR from the windows contextual menu - java

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.

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.

How to find out what happens when I double-click .jar file on Mac?

There is a .jar file that runs AEM (Adobe Experience Manager) Author instance once I double-click it. I wanted to run it from command line java -jar cq-author-p4502.jar, but it does something differently than when I double-click it. It serves me a web page for product registration.
So, I assume that double-clicking cq-author-p4502.jar doesn't do the same thing as java -jar cq-author-p4502.jar. Where could I find out what is being executed on double-click on .jar file on Mac?
In Finder, right click the jar file and select Open With, then take a good look at what is selected as the default application. On El Capitan the default is Jar Launcher.app
On OSX there is a command line tool called open which does almost the same thing as double clicking in Finder. Perhaps the way to get the behavior you want from the command line is this.
open cq-author-p4502.jar -a "/System/Library/CoreServices/Jar Launcher.app"

Export runnable jar files

Need to export clickable and executable file in eclipse. Simple exporting does not launch the program. How to get a clickable jar?
You should click file, then click export, then click java and save it as runnable jar file.
To run the .jar file, open cmd or terminal, make sure you are in the right path and
type java -jar <jar-file-name>.jar
Note if you cant run it, mostly its your file path not correct
Try to go in brief using the below link :
http://www.wikihow.com/Create-an-Executable-File-from-Eclipse
The code listing represents a command prompt application; you can only make it do anything when physically run on the command prompt / shell itself, especially because it expects user input that way. The reason why turning the given program into an executable jar will result in "nothing" happening when you double click it, is because under Windows the default binding for the 'jar' filetype will be setup to run through javaw.exe - and javaw.exe is designed to not open up any command prompt.
In order to make a program be able to run without the command prompt so you only have to double click on it, you will need to get rid of the command prompt interface and instead build a proper graphical user interface, using for example JavaFX or Swing.

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

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.

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.

Categories

Resources