Let jar file run in cmd or terminal on mac - java

The program I write in Eclipse runs on console purely. I hope to export it to a jar file then use launch4j make it an executable .exe file. The program should desirably be opened in cmd or terminal on Mac once clicked. But it seems I can't open it with cmd and unless I used swing or awt, nothing to show when I clicked either jar file or exe file. So what is the problem and how should I do to make it runnable on cmd!
PS:I dont want to type any command line, just click the jar file or exe file and it started to run on cmd or terminal!

You would possibly try script files for this task:
windows: run.bat
java -jar your.jar
mac: run.sh
#!/bin/bash
java -jar your.jar

launch4j could work on windows/osx/linux, but I think you need a mac to build the executable file. And the jar file itself can be run when double-clicked, if you specify the Main Class in META-INF/MANIFEST.MF file, like this:
Main-Class: com.xxx.xxx.YourClass
After this, the jar file could be double-clicked to run.

This Answer shows the use of Jar Bundler to convert jar to executable native Mac OS X .app executables
I think you can give it a try. Enjoy!
Edit
Jar2Exe seems promising to convert jar file to executable for Windows and Linux Platform.
You can try them and let me know too if that work for you.

Related

.Jar is not opening

I'm having trouble opening a jar filewith java. I've tried uninstalling java and reinstalling it(and different types of java, JDK) and have tried opening the file with .java, .javaw, and .javaws in both the jre and jdk files in the java folder. When I use the .java it opens a black command console for a split second and then closes. I'm running windows 8. I have ran the same file on other pc, my pc doesn't want to open the .jar file. I am using intellij idea. Thanks in advance!
You get an error running the jar file. It displays for a split second and the it exists.
Try running it from the command line. That will give you more insight in to the error that prevents you from running the jar file.
Go to cmd (press Windows key and type 'cmd', press Enter). Navigate to the directory where you have the jar file and execute
java -jar yourJarName.jar
Substitute 'yourJarName' with the name of your jar file. Now you should be able to see the error, and we can work on it from there.
Have you tried to run it from console
> java -jar your_jar_file.jar
To see what could it be wrong?

Running a jar2exe file as jar from command line

I am trying to run the NetBeans exe without having a JDK installed to the system, however, I have the 1.8 JDK unzipped to a flash drive. When trying to run the NetBeans exe as a normal jar file using
E:\Java\jdk1.8.0_60\bin\javaw.exe -jar E:\Java\NetBeans\bin\netbeans64.exe
it says that the jar file is corrupt or invalid. To my knowledge, the exes made from jar files are just jars wrapped to an exe, so I have no idea why it wouldn't work this way.
EXE files cannot be ran using java -jar or javaw -jar command only JAR files can be execute like that.
If you want to use mention JDK that netbeans runs on use netbeans.exe --jdkhome <jdk home>
In your case the command will be
E:\Java\NetBeans\bin\netbeans64.exe --jdkhome "E:\Java\jdk1.8.0_60"
Netbeans uses Java environment to initiate its installation. Exe and jar are two different files you cannot run exe file via javaw.exe -jar

How to make a Java program be opened by default with Java, on any computer

When I send Java games I created to friends, the JAR file I send them often looks like a RAR file to them. This is because Windows on their computers thinks this file should be opened by WinRar.
They have JRE on their computers, but the default on Windows is that the file they recieve should be opened by WinRar.
The people I send my program can't be expected to guess, that what they need to do is specifically tell Windows to open the file with Java.
Is there a way to make it so that a JAR file I send to somebody, will be opened on his/her computer by default using the JRE?
Thanks a lot
You can consider adding a BATCH script.
Something like java -jar YourJarName.jar
You will need two of them - for unix and windows:
Windows start.bat:
java -jar YourJarName.jar
Unix start.sh:
#!/bin/bash
java -jar YourJarName.jar
You could use a tool such as Launch4J to wrap the JAR file into an executable.
You can convert your JAR file to executable file using Java Launcher.
OR
As #lan Roberts answer You can use Launch4j to wrap your JAR to executable. You can download it from this.

Executable jar doesn't start normally

I created an executable jar file with eclipse indigo, but when i double click on it nothing happens. But when i write java -jar c:/dir/filename.jar to cmd, it works. Other jars runs well. I'm using jdk 1.7.0_02 and eclipse indigo. How could i start it normally?
You can run a jar by double clicking it, you just have to make sure that Java is associated with .jar files. The easiest way to do this is by reinstalling the JRE (which generally does it for you).
Otherwise you will need to modify some of your Windows properties (I'm not sure what version of Windows you are running) to point .jar files to the instance of javaw.exe.
Here's a link to a Windows 7 page on how to do it.
right click on .jar file and go to properties, then set "open with" program to "Java SE Binary".
You can do this by associating the .jar extension with the command to run it via javaw.exe.
"C:\Program Files\Java\{java version}\bin\javaw.exe" -jar "%1" %*

Executable JAR on Ubuntu (NetBeans)

I'm writing a simple Swing application in NetBeans and doing so on an Ubuntu machine for the first time.
As many of you know, NetBeans automatically creates executable JARs for projects that are "set as main".
On Windows, you can double-click an executable JAR and it automatically invokes the JRE and runs the app. In Ubuntu, double-clicking the .jar file causes the file to be opened in the archive manager instead. In order to run my JAR, I either have to right-click it and select "Open with OpenJDK Java 6 Runtime" or launch it from the command line.
From the command line I get no problems whatsoever. However, when I try launching it from the right-click menu, I get an error that reads:
The file MySwingApp.jar is not marked as executable...
So I have 2 questions:
What do I have to do to set it as executable? Is this something I can do inside NB or do I have to use the shell? If I have to set permissions via the shell, doesn't that conflict with NB's policy of auto-generating **executable** JARS? And what command would I use to flip the executable bit anyhow?!?!
Is this just a Linux hiccup? I want to send this JAR to friend who run Windows and I'd like for them to be able to just double-click it and have the program launch
Thanks for any helpful suggestions!
You will need to manually tweak your build process to get the jar file marked as executable in Netbeans. Go to your project root and open build.xml. The header has instructions on adding to the build process. There is a target, "-post-jar", that is called after the jar is built. You'll need to make that target and use Ant's chmod task to modify your jar. Once you do that it will occur every time you make a jar file in that project.
It will run fine on your friend's Windows machine, as long as he has a JRE installed.
Here is a thread about running jars using double click in Linux.
You can Java like a native binary on Ubuntu (and other linuxes), it's a feature of the kernel. You need to install the binfmt-support package to give the kernel the hooks to run java in this way.
In Ubuntu open up a terminal and run:
sudo apt-get install binfmt-support
Then make your JAR file executable
chmod a+x yourjar.jar
Then you can run your JAR like any other binary by typing
yourjar.jar
Jar-files aren't first class executables, and they don't become magically executables by changing their executable flag.
If you execute a jar, you run the command
java -jar YOURJAR.jar ...
It's the same, as if you double click a png file, and expect it to run in a painting program
gimp YOUR.png
You don't need to make your png an executable one, and it will not solve a problem.
Instead, you have to tell your desktop environment, what to do when double clicking a jar or png-File, and you have to do it on Linux the same way you do it in Windows - maybe the installer on Windows does it for you, because there is normally just on Desktop Environment (Windows) on the OS (Windows), but Linux has Gnome, KDE, XFCE, LXDE, fluxbox and millions more.
And it isn't so sure what you want to do with it. Since jar-files are a special form of packed zipfiles, usually containing a Manifest and the classes, the Archivmanager isn't a false solution, and it is saver to show the content of the archive, than executing it.
Copying the file to windows has no effect. Windows not even has an executable flag, but you shouldn't fiddle with it though. You change your desktop settings, and those can't be moved to windows, and you will not want to.
And if you have the correct settings in your DE, you don't need to tell Netbeans or any other IDE repeatedly, what to do with jar-files.
I gone through Internet and I came across one article with complete steps to run jar file
http://mlartist.blogspot.in/2012/07/deployment-netbeans-project-in-linux.html
Jar files are basically a zip file, to create an executable, you have several different methods. The Best (in my opinion) is to use ant to create it. Or you can simply echo "Main-Class: YOUR.MAIN.CLASS" >> Manifest and then create your jar by jar -cmf Manifest JARFILENAME.jar INPUTFILES then, to make it executable under linux, right click on it and click on properties. Then click on permission tab and check execute. or you can be a terminal bamf and cd to the jar directory and chmod +x JARFILE.jar
HAPPY NIXING!!

Categories