Executable JAR on Ubuntu (NetBeans) - java

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!!

Related

Izpack executable jar to RPM

I'm trying to create a rpm package for an executable jar generated using izpack. It's basically an installer wizard that allows a user to install a tool/software. It provides an executable jar. So we have to do
Java -jar file.jar
To initialize the wizard. I'm trying to automate it in Linux by creating an rpm that takes this jar and an sh file that executes this jar and runs the sh file when I execute the rpm file. I'm new to rpmbuild and all the tutorials online are about copying a particular jar from the rpm file to a target location not about how to execute it.
Can someone provide me a sample spec file which does the following
Creates an rpm file with .jar and .sh
When rpm -i file.rpm is executed, it runs the .sh file which in turn executes the jar and opens the wizard.
I'm open to any other suggestions on how to make jar auto execute. I'm basically trying to create an exe similar file for linux
Thanks in advance.
I highly recommend not doing this for various reasons, because it is the exact opposite of what RPMs are supposed to do. You should instead do the installation process on a build machine (as a non-root user!) and then capture the files to be installed.
Why is your approach a bad idea?
Installation script is hidden from the user (they cannot just query the RPM scriptlet to see what changes, beyond files, you made to their system)
The final installed files won't be controlled in any way, e.g. you won't be able to verify if they had been corrupted (rpm -V) or query what package installed them (rpm -q --whatprovides /path/to/file)
"Removing" or "Upgrading" the package won't actually do anything unless you write detailed scripts to handle all these unknown files
All the other reasons I didn't list as why you use a package manager instead of blindly distributing tarball distributions like we did in the dark ages
That said, if you insist on breaking things, your solution is to put the java installation call in %post, which will execute after the files have been installed. It will call it again when you upgrade the package, so the other installer needs to handle that. But again, this is not the right approach.

Completing My Project

I have finished writing a project in the IntelliJ IDEA using Java. I can not figure out how to get the project from a saved file in the IDEA to an executable on my computer (I am on Windows 10 if that matters). Then, I want to be able to put the executable on my Girlfriend's computer (mac) because it's a gift for our anniversary. She's not very tech-savvy (apparently neither am I...) so I want to be able to just drag and drop something onto her desktop and have her be able to double click it and have it work without having to install anything to her computer. (I was looking at Jar files, but something I read said that mac's do not run Jar files unless I install some things).
So what I'm really asking is:
1)Using IntelliJ, how do I finalize my project (make a JAR file)? I have been unable to find directions for this that have actually worked.
2)What file do I need to make a batch file call to get it to run my program once it is a JAR? Does it just call the JAR as a whole, or a specific .class? (Someone suggested a bash file, but I have not heard of this before, so I am open to other suggestions besides batch files. I am currently looking into what bash files are)
3)Can I make the batch file call the file indicated in question 2 via relative path, thus allowing me to keep it in the JAR or in a folder with the JAR and making a shortcut to it on a desktop (for example)?
Building a JAR in IntelliJ
Option 1 - Long way round, getting IntelliJ to build your JAR
Hit Ctrl+Shift+Alt+S to open your Project Structure settings menu.
Go to Artifacts
Click the green +
Select JAR > Empty.
Name your JAR, for example Gift
Right click on all the files in the Available Elements that you want to put into your JAR (often everything) and select Put into Output Root
Click Apply. Click OK.
Go to Build > Build Artifacts... > Build.
You'll find your JAR in out/artifacts/{JAR_NAME}/{JAR_NAME}.jar
Option 2 - Short way (Maven only)
If you're using a dependency manager like Maven and you've adhered to the Maven project structure, just use the command:
> mvn clean package
and you'll find your JAR in your target/ directory.
In either option, you may have to define your .class file that contains your main() method.
Running a JAR
The normal way to run a JAR file would be to go into cmd (Windows) or a terminal (Mac & Linux) and use the command:
> java -jar {JAR_NAME}.jar {args}
where {JAR_NAME} is the file name of your JAR and {args} are any arguments that need to be defined up front.
If you want to not use a command line you could write a script that runs the JAR for you. Then you can double-click/run the script and it runs the JAR for you.
For Windows
I'd recommend writing a simple .bat or "batch" script that will run the file for you.
Keep the batch script in the same directory as your JAR file and just have it contain:
java -jar ./{JAR_NAME}.jar {args}
You could use a Powershell script or something similar though.
For Mac/Linux
As Mac (and Linux) are Unix systems we can use a .sh or "bash" script to run the JAR. Again keep the bash script in the same directory as your JAR file and use:
#!/bin/bash
java -jar ./{JAR_NAME}.jar {args}
.bat script is exclusive to Windows systems, .sh scripts are exclusive to Unix systems.
Because Windows and Unix are two very different systems, you're not going to find an executable file that works for all systems.
Converting your JAR into an executable
You could convert your JAR into a standard Mac "app" application bundle by following some instructions here.
However, I wouldn't recommend this as it isn't really necessary when you have a simple script/command option and for this method, you'd most likely need access to a MacOS machine with development tools.

How to execute GUI Java application?

I have made some GUI Java application and exported it as "Executable Jar". But Windows shows me this file as RAR archieve, and I can't execute it! How can I fix it?
From the command line, run: java -jar <jarfile>.
From Windows Explorer, right-click on the JAR file, choose "Open with..." and select your local Java installation.
Make sure you have a JRE installed. Normally Windows will recognise JAR files and do the right thing with them. The fact that they are being identified (understandably) as an archive would hint that you have no JRE.

jar to exe convert problem

hi i converted my jar file into an exe using jsmooth but when i install it, it shows an error like java not found.
Please help me, how can I add the jre to my exe wrapper so this problem is solved.
It is trying to get the java installation to run your application, where it might be searching in JAVA_HOME, So if the JAVA_HOME is not set in the machine where you installing your application then try to install the java and set the JAVA_HOME as a pre-requisite of your installation.
I don't believe JSmooth can actually bundle a JRE with the exe. What you can do is tell JSmooth where to expect the JRE when running the exe (as in the same folder the exe is run from). If you do this you simply need to zip the exe and a JRE up, and distribute that.
The end user would unzip this, and the resulting folder would contain your exe and the JRE. Since JSmooth knows where to look relative your exe, it can find the JRE.
i have use it using launch4j
follow the following steps
1-create project directory called e.g:project
2-copy the runnable jar file to it
3-copy the jre directory to it u can rename it or keep name as it e.g i will call myjre
run launch4j program :
1- fill the basic tap with required information .
2- go to JRE tap there is a field called (bundle JRE path ) write "myjre" add min Jar virsion 1,6 it will case you an error if you do not fill this fields
and then click run button to generate exe file , you have to remember to keep the myjre directory with along side with executable exe file
e.g the application directory should contains :
1- [you app name].exe
2-myjre
in this description you can run your application with no care if jre is installed in a machine or not and become portable
Good Luck , feel free to contact me for more details

Run Java applications By double click

why i can`t run my java compiled applications with double click?
my applicatins just run in cmd (java -jar: my app.jar)
plz help me
You can make a batch file to run your command. This way it will work without the need to make associations to .jar files. If it is a windows app then start it with javaw.exe.
You need to associate the .jar extension with javaw.exe, then no console window will be shown, when you double click the jar file
This is usually done by the JDK/JRE installer, so either you didn't install the JDK properly, or you manually changed that associaten.
I think all the answer here are valid. But if you want a true executable file in Windows then you need to create a launcher. In windows, jar files are not executable. But exe files are. There are many tools out there than wrap all your java code around some native library. I recommend http://launch4j.sourceforge.net/ I think this is what you are looking for. You can easily create executable with icons and many more features.
You can create a Windows shortcut to your .jar file.
The following line could be the target of the Windows shortcut.
%windir%\system32\javaw.exe -Xms1m -Xmx1m -jar "C:\Program Files\Jar Directory\file.jar"
This has the advantage of allowing you to specify the javaw parameters that you want, as well as the Windows parameters (normal window, maximized, start in directory) that you want.
Just assign .jar files to be executed by javaw.exe. You can do that using the Open with dialog on a .jar file.
BTW, doesn't the Java installer do that for you?

Categories