Exporting my Java program in Eclipse - java

I have made a hello world program in eclipse and I was wondering how to run it out of eclipse (not on the console). I want to run it like you would with a .exe. Is this possible?

If you exported your program as an .exe, you would only be able to run it natively on Windows (the point of Java is that it is cross platform). You want to export it as a Runnable JAR File, which should be one of the export options in Eclipse:
By default, no console window will be opened when you try to open your JAR file, so any calls to System.out or System.err will be invisible. You can either run your JAR file through command prompt (java -jar YourJarFile.jar) or create a GUI for your user to interact with.

Related

Create an Exec File in Eclipse

I'm sure the answer is really obvious and right in front of my nose, but I am writing programs in Eclipse and I'd like to be able to run them outside of the workspace. (Like on the command line.)
Problem is I can't find the executable file in the file explorer. So I have to ask....
How do you run programs you write in Eclipse OUTSIDE Eclipse? (Program is in java.)
I just recently switched from Visual Studios to Eclipse so I don't really know Eclipse well. Most of the time figuring out the IDE is more hard to actual coding really.
The executable can be created by Clicking: file -> Export -> As Runnable Jar File
Then make sure launch configuration is set to the correct project, and that the export destination is where you want it to be. Then Click finish, and your runnable jar file should appear where you specified.
If the program has a GUI, it can be run like a regular exe file (double clicking, etc.) if it does not have a GUI it will need to be launched from the command line (ie, by navigating to it with CMD, or with a batch file).

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.

how to run java program outside eclipse on a server that doesn't have eclipse

I wrote a java program that contains many classes in eclipse, it is my first time that I write a code in java and in eclipse too, now I need to run this code on a server that doesn't have eclipse.
Compiling & Running a Simple Program.
This is your best starting point.
Use "File -> Export -> JAR file" wizard to create a .jar file contained your compiled classes. You can then copy this .jar file to your server and use java.exe to run it. Use "java -help" from a command line to get instructions.
Please go through link provided by Andrew
Eclipse is an IDE , it facilitates development but not actually required to run your code, if you want to run your java file in a server somewhere, make sure you have JRE installed in your "server" and in your Path, then run following command in your server's shell environment
javac filename.java
above command will generate a .class file in the same directory, which is actual file that can be executed using following command,
java filename
assuming your code is inside a file called filename.java

Creating a stand alone Java application

Is it possible to create a stand alone application in Java (using Swing) and package it into an exe file?
My Idea is, we should be able to double click it and run the application.
If you export a runnable .jar file you will be able to double click and run, but you'll still need the JVM in order to interpret the bytecode.
The best way to deploy a Swing based app. to a client's desktop is using Java Web Start.
JWS offers desktop integration - making a desktop shortcut and menu item, each using an icon if specified. The end use can double-click the desktop shortcut cut to launch the app. ( or single-click the menu item to do the same ;).
Best of all, JWS works for all platforms for which the J2SE is supplied. Windows, Ubuntu, Fedora, Solaris, Mac. OS..
For ensuring the user has the correct minimum JRE needed to run the app., use deployJava.js to write the link to the JNLP launch file.
If you export your project in a .jar file, you will be able to run it when you click on it.
I suppose that you know that you must have the virtual machine installed in the computer that runs the program.(The virtual machine is the thing that allows java to be operative system independent)
And also you must know that if you transform that .jar to an .exe file using some kind of gadget... Then java will not be java anymore, because will not be able to run it in non-windows operative systems.
Remember java slogan
'Write once run everywhere"
See this image here i will explain you how to create a .jar using the eclipse IDE:
1-Select your project in the package explorer
2-Select File>Export from the menu
3-Pick Runnable JAR file from the dialog and
4-Click next and follow the rest of the wizard
You can package your application to JAR file. Application in jar file runs on duoble click.
Here is tutorial. http://download.oracle.com/javase/tutorial/deployment/jar/
You can already do this with a normal .jar file if the user has configured the extension to be executed with the JRE. But you can always use something like Launch4J. Please Note this simply wraps the Jar in a EXE file. The file still needs your Dependencies (.jars) and the JRE enviroment.

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