I wrote a Java GUI program in Netbeans IDE 8.0.1 called SampleChat and used the 'clean and build' function to create a jar file.
I went to the 'dist' directory that Netbeans created and double-clicked the jar file it produced.
A cmd window opened with the following line:
"Error: could not find or load main class.
However, when I opened cmd, changed to the same 'dist' directory and typed the command:
java -jar "SampleChat.jar"
the program ran just fine.
Can I not use Netbeans to create jar files that execute on double-click?
I did not use any pre-existing jar files to complete the program, hence, there was no 'lib' directory or anything like that.
I intend to share programs from a USB stick so it is vital that the only requirement is a JVM, mouse and maybe some fingers to double-click it.
An awkward solution I came up with is to write a batch file that runs the program, but it requires the jar file to not be moved around - screw that.
So how do I make my programs double-click executable?
I guess you are missing to create a manifest file. Therefore it's unknown what your mainclass is. In your java command executed from a terminal you defined the mainclass to execute.
From the tasks you mentioned I guess you are using gradle to build your jar?
Here http://www.gradle.org/docs/current/userguide/java_plugin.html in chapter 23.14.1
an example of an manifest is given.
If you add attributes 'Main-Class': 'com.project.MainClass' it should work.
BR
Related
I have created a simple SWING gui for a cmd program someone else developed. To run this program I execute this line:
Process convertProcess = Runtime.getRuntime().exec("jlyt\\prog\\com_win\\jlyt.bat " + selectedFiles.getName());
The jlyt folder is in the same folder as my src folder (I am using IntelliJ).
When running via IDE everything works great, but not when I run the jar I created. I have tried running it from the directory it was saved to by IntelliJ as well as from the directory of the jlyt folder.
I did not add the external program (inside the jlyt folder) to my jar since it is very heavy. I want my jar to be distributed along side the original program and not to contain everything.
Any idea how I should build my jar?
Thanks.
I see why you put /jlyt in /src, it serves as a convenience within the IDE. /jlyt will get copied as a resource into /bin/classes, or whatever the IDE target is, and that allows everything to work from the IDE.
When you JAR your application, /jlyt is typically added to the JAR; however, it is not accessible to Windows. I assume you are placing a copy of /jlyt in the same folder as the JAR when you attempt to run.
As a first step, in a terminal, set the current directory to the folder containing the JAR and /jlyt. Since you are specifying a relative path in your exec(), that should be sufficient for everything to run.
You can also try creating a shortcut to the JAR, since it is executable, and set the working directory to the folder containing the JAR.
You have to use a relative path based on the place where jlyt.bat is according to your jar file.
e.g. use "./" or "../" to navigate the directory tree up.
The location of the JAR file is only relevant for starting the JAR. The working directory must be the directory that contains the jlyt folder, since you are using 'jlyt\...' as path to the executable.
Example, lets suppose following directory structure:
somewhere
project
gui
appl.jar
jlyt
prog
...
working directory must be 'project' and the JAR will then be referenced as 'gui\appl.jar
C:\somewhere> cd project
C:\somewhere\project> java -jar gui\appl.jar
Also be sure to wait for the convert process to terminate (e.g. convertProcess.waitFor()) before exiting your application/java - I believe that any running external process is killed when the Java Virtual Machine is closed!
Hint in documentation of Process:
As of 1.5, ProcessBuilder.start() is the preferred way to create a Process.
I'm having troubles with my jar files here. I'm kind of a beginner in Java and I wanted to make a .jar file in order to share my little first program with a friend of mine.
It's a 2 classes program, they depend on each other and work together when compiling and running. I created a MANIFEST.MF file and created a .jar using the command line.
All seem to work, when I type: java -jar myApp.jar it works. But when I double-click my myApp.jar file, I expect a terminal window to open and print out some things and nothing happens...
(I'm on Mac OS X Yosemite)
I'm using IntelliJ to create JAR file, and it executes normally when run from command line, but on double clicking it, nothing happens. The JAR I export from eclipse work normally, and since I'm a beginner using IntelliJ my guess is that I am doing something wrong.
I'm exporting the JAR in this way:
Project structure -> jar -> from modules and dependencies then use this
configuration to build.
Not sure why everyone is playing dumb about self executing jars.
Just add a MANIFEST.MF file and specify the fully qualified main class as Main-Class: my.package.MyClass
Also, make sure your executable type for .jar file type is Java
Once you do this you can just double click the jar to execute it.
Intellij will not execute a JAR for you. Remember JAR is a container, Is the same as if you were asking to execute a ZIP file.
What I am sure you want is to execute a class inside your jar file. Which would be the class which has the:
public static void main(String[] args) { ... }
You have two options:
1) execute the jar from command Line
2) right click on the "main" word I wrote above and then press RUN.. You will see a small green arrow (play button). That is the only way of running a class inside Intellij.
Hope it helps
If your JAR starts from the command line using jar -jar <file>, that's as executable as a JAR file gets (in other words, metadata specifies where the main class is). You can if you want use the OS provided tools to specify that when you double click on a .jar file, it should execute java -jar <file>, but that's beyond the scope of JAR files. If you're running Windows for example, you should be able to achieve this by following the procedure outlined here. Again, this is OS specific and may even be specific to which version you're running, and is essentially just a fancy wrapper around the double-click event to execute the java -jar command.
Make sure that the x-flag is set for the jar.
java -jar xyz.jar
does not need that, but double click need it:
ls -l xyz.jar
chmod 755 xyz.jar
This is more curiosity than a problem:
I was recently wondering if there was a way to run compiled Java applications without using the cmd or an IDE such as Eclipse. I use Eclipse, but it isn't very useful if you want to run the program independently. Can you save Java files in Windows Explorer so you can create a shortcut for them? If so, how? Is there some sort of special extension to the file? I've heard of .JAR files, but I'm not sure what they are. Can anyone tell me how to do it?
.JAR files are archives containing - amongst other things - your compiled classes and a manifest file. You may set the main entry point of your application in that manifest. See Setting an Application's Entry Point.
Normally if you double click a jar file in windows it will be opened by javaw.exe -jar <yourFile.jar>. javaw.exe will lookup the manifest and try start the main class defined there.
create the jar file for java application using following syntax jar -cvf .jar . then use javaw.exe -jar
This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 8 years ago.
I have programmed a Java Program in JCreator, everything is done, but I want to create an executable file from it, ie I dont want to have to run the program by loading the java classes and compiling then executing, but instead have it as a stand alone executable file.
What the quickest way to do this?
You can use the jar tool bundled with the SDK and create an executable version of the program.
This is how it's done.
I'm posting the results from my command prompt because it's easier, but the same should apply when using JCreator.
First create your program:
$cat HelloWorldSwing.java
package start;
import javax.swing.*;
public class HelloWorldSwing {
public static void main(String[] args) {
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
frame.add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
class Dummy {
// just to have another thing to pack in the jar
}
Very simple, just displays a window with "Hello World"
Then compile it:
$javac -d . HelloWorldSwing.java
Two files were created inside the "start" folder Dummy.class and HelloWorldSwing.class.
$ls start/
Dummy.class HelloWorldSwing.class
Next step, create the jar file. Each jar file have a manifest file, where attributes related to the executable file are.
This is the content of my manifest file.
$cat manifest.mf
Main-class: start.HelloWorldSwing
Just describe what the main class is ( the one with the public static void main method )
Once the manifest is ready, the jar executable is invoked.
It has many options, here I'm using -c -m -f ( -c to create jar, -m to specify the manifest file , -f = the file should be named.. ) and the folder I want to jar.
$jar -cmf manifest.mf hello.jar start
This creates the .jar file on the system
You can later just double click on that file and it will run as expected.
To create the .jar file in JCreator you just have to use "Tools" menu, create jar, but I'm not sure how the manifest goes there.
Here's a video I've found about: Create a Jar File in Jcreator.
I think you may proceed with the other links posted in this thread once you're familiar with this ".jar" approach.
You can also use jnlp ( Java Network Launcher Protocol ) too.
If you are using Eclipse , you can try the below 7 steps to get a .exe file for Windows.
Now you have a JAR file. Use java -jar path/jarname.jar to execute.
If you want to convert this to .exe, you can try http://sourceforge.net/projects/launch4j/files/launch4j-3/
STEP7:
Give the .xml file an appropriate name and click "Save". The .xml file is standard, don't worry about it. Your executable file will now be created!
I'm not quite sure what you mean.
But I assume you mean either 1 of 2 things.
You want to create an executable .jar file
Eclipse can do this really easily File --> Export and create a jar and select the appropriate Main-Class and it'll generate the .jar for you. In windows you may have to associate .jar with the java runtime. aka Hold shift down, Right Click "open with" browse to your jvm and associate it with javaw.exe
create an actual .exe file then you need to use an extra library like
http://jsmooth.sourceforge.net/ or http://launch4j.sourceforge.net/ will create a native .exe stub with a nice icon that will essentially bootstrap your app. They even figure out if your customer hasn't got a JVM installed and prompt you to get one.
On the command line, navigate to the root directory of the Java files you wish to make executable.
Use this command:
jar -cvf [name of jar file] [name of directory with Java files]
This will create a directory called META-INF in the jar archive. In this META-INF there is a file called MANIFEST.MF, open this file in a text editor and add the following line:
Main-Class: [fully qualified name of your main class]
then use this command:
java -jar [name of jar file]
and your program will run :)
Take a look at WinRun4J. It's windows only but that's because unix has executable scripts that look (to the user) like bins. You can also easily modify WinRun4J to compile on unix.
It does require a config file, but again, recompile it with hard-coded options and it works like a config-less exe.
Jexecutable can create Windows exe for Java programs. It embeds the jars into exe file and you can run it like a Windows program.
You could use GCJ to compile your Java program into native code.
At some time they even compiled Eclipse into a native version.
Take a look at launch4j
Write a script and make it executable. The script should look like what you'd normally use at the command line:
java YourClass
This assumes you've already compiled your .java files and that the java can find your .class files. If java cannot find your .class files, you may want to look at using the -classpath option or setting your CLASSPATH environment variable.
Java Web Start is a good technology for installing Java rich clients off the internet direct to the end user's desktop (whether the OS is Windows, Mac or *nix). It comes complete with desktop integration and automatic updates, among many other goodies.
For more information on JWS, see the JWS info page.
As suggested earlier too, you can look at launch4j to create the executable for your JAR file. Also, there is something called "JExePack" that can put an .exe wrapper around your jar file so that you can redistribute it (note: the client would anyways need a JRE to run the program on his pc)
Exes created with GCJ will not have this dependency but the process is a little more involved.