Create an Exec File in Eclipse - java

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).

Related

Java .jar file Run Differently from Command Line and File Explorer

I recently wrote an application that is packaged as a .jar file. When I run the application from the command line via "java -jar MyProject.jar" everything looks perfect, just as it does when run from within eclipse. However when I go to the file location in the file explorer and double clock the file, it doesn't quite work. All of the functionality is there, but the GUI is not quite right. All text is much larger, despite the application not changing size, when run like this, to the point where much of it is cut off.
Anyone know whats going on? Thanks!
The cause of this issue was that when being run from the command line, the program was being run with JRE 1.8.0_172, however when being run from the file explorer, the program was using JDK 10.0.1

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 run a java executable file on other computer

I made a small application using Java Swings and then created a jar file of the project using 'clean and build' option in Netbeans. Then, I converted that .jar file in a .exe file using the software 'Launch4j' and it was perfectly running on my laptop. However when I tried running that exe file on other laptop. It displayed the error: Class NewJframe.firstfile couldnot be found.
( I made the database on the other laptop also with MySql Connection).
Please help me clarify why I am getting this error.
Also, I would like to know what all are the requirements to run an .exe file on other computer which I made from a .jar file?
I would pretty much expect something like Launch4j to take care of all your packaging requirements; did it give you options to "include all dependencies" that you didn't click "YES" to...?
It sounds like it's packaged your app, but possibly not the Java runtime envt it needs to run your app.
Try giving him the JAR file and see if he can run that. If he can, It's a problem with Launch4j, if he can't it's a problem with Java.
Alternatively try something like Excelsior JET instead.
Class NewJframe.firstfile couldnot be found
Launch4J only bundles your application's main JAR file inside the .exe (or not even that if you select "don't wrap JAR") - any other JARs that the main one depends on must be present in the same relative locations, you can't necessarily copy just the .exe.

Exporting my Java program in Eclipse

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.

Eclipse Project Compiling, Or Not?

I noticed that when I build even a very simple Java program in Eclipse, and I try to run it from the Terminal/Command Line and it gives me errors. I noticed after some hunting around that I have to actually compile the .java file I created in Eclipse in the Terminal to create and run the application. However, I can just save and run in Eclipse and get the same output (within eclipse).
I checked to see if I could build the project in Eclipse but the option to do so is greyed out. So, how can Eclipse run it if it actually never gets compiled?
Eclipse does compile it, otherwise it could not run it :-).
Eclipse generates normal .class files, just like javac. It puts them into its "build directory", which you set in the "build path" (or something - Eclipse not handy right now) dialog. By default its under /bin, I believe.
In principle, you can run your program in a terminal using these class files; you just need to set your CLASSPATH accordingly.
In practice, you would either run your program from inside Eclipse (which is for example easier to debug), or deploy your program (using e.g. Ant) to get some kind of installer or installation file, then install that and run it. That way you always run from a complete, correct installation.
Of course for small/simple programs, just running from Eclipse's class files is quite ok.

Categories