Export runnable jar files - java

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.

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"

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.

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.

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.

Categories

Resources