So, since the jar generated by Netbeans requires the lib folder that it also generates, how do I go about adding the lib folder to the generated jar and change the Manifest so that it launches without any command line usage?
..so that it launches without any command line usage?
If it has a GUI, launch it using Java Web Start.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration1, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
Desktop integration: desktop shortcut & menu item on supported platforms - no more Command Line based launch!
See Enabling Java Web Start in the NetBeans IDE to get started.
Although there are many ways to do it, they mostly involve modification of the Ant build.xml. This is the best tutorial I've found so far. FYI, there are other SO questions that are very similar.
NetBeans IDE handles all these automatically.
New Project -> Java / Java Desktop Application
Press next, fill everything and also check "Use dedicated folder for storing libraries", press finish
Go to the Files view, double click build.xml and from the "Ant targets" list, find the "jar" target, right click "Run target".
This way the generated .jar file will contain all dependencies and have a manifest with the Main-Class set:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.1
Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
Main-Class: desktopapplication1.DesktopApplication1
Class-Path: lib/appframework-1.0.3.jar lib/swing-worker-1.1.jar
Edited: this does not package libraries into the .jar, but puts them next to it in a separate lib. See #user845279
I ended up using ninjacave.com/jarsplice to compile it all (libraries included) into one executable jar. (I got an error with the background color being the default color even though I had set it to a different color, so I modified the code that jarsplice uses to launch your program using java rather than the path to java)
Related
I have an application that works fine and that I can execute from a .jar file. But I want to know how to make it runnable from any computer even if there is no JRE or the JRE version is not the good one. I thought about a .exe file but don't know how to do it.
I made my code with Eclipse and it use jxl,jdom and jfx librairies.
I tried to use Inno Setup 5.5.6 but when in Eclipse I run my build.xml as Ant Build it return me an error that says :
"'Launching JFX Build - Myapp' has encountered a problem.
The achive : C:/Program%20Files/eclipse/plugins/org.eclipse.swt.win32.win32.x86_64_3.104.0.v20150528.jar which is referenced by classpath, does not exist."
JavaFX provides specific utilities for bundling your application as a native package, which is referred to as "Self contained application packaging". These utilities are provided as a command-line tool, or as ant tasks. Additionally, the common IDEs support this via wizards, either out of the box (NetBeans) or via a plugin (e(fx)clipse for Eclipse). The details of how to use any of these are far beyond the scope of a stack overflow question, but the basic documentation is available here. A tutorial for e(fx)clipse is here.
Briefly, for e(fx)clipse, you should double-click the build.fxbuild file that is created in your project. Under the "Overview" tab, find the "Packaging format" option, and select "All" (or the specific type of package you want to create).
Note that you can only create a package targeted at the platform on which you are building, so if you want to create packages for windows, Mac, and Linux, you will need access to all three types of machine.
I know I am late. I faced same problem a trick worked for me. Look at the given directory in the error message. It contains a space (C:/Program Files/...). Move your e(fx)clipse to another directory where the directory doesn't contains any space.
Not sure it will work your everyone or not but it worked for me.
Thanks
There are various tools that let you wrap your Java application in a Windows executable. Some tools are only simple installers, others allow you to bundle your application with a specific JRE version.
A widely used but commercial tool is install4j, but there is a bunch of other tools, such as WinRun4J.
I wrote a program in NetBeans, and now want to share it with my coworker. However, when he tries running it on his computer, he gets an error message:
"Could not find the main class: excelcomparator.ExcelComparator. Program will exit."
Here's the confusing part: I wrote this on my laptop which has NetBeans, and to make sure that it worked, copied the dist folder onto a flash drive, and ran it on my computer. It worked fine. When I emailed it to my coworker, he got that error.
Based on what I've seen, some solutions are to run the .jar from the command line. While that might work, I need the file to be double-clickable.
I sent my coworker the file via email in a zipped folder, is it possible that unzipping the entire folder also messed up the .jar file (don't see why it should, but included anything that might help)?
This is the MANIFEST.mf that's within the .jar file. There is a carriage return at the end of the file, it just doesn't copy well into this text box:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_71-b14 (Oracle Corporation)
Class-Path: lib/poi-3.12-20150511.jar lib/poi-examples-3.12-20150511.j
ar lib/poi-excelant-3.12-20150511.jar lib/poi-ooxml-3.12-20150511.jar
lib/poi-ooxml-schemas-3.12-20150511.jar lib/poi-scratchpad-3.12-2015
0511.jar
X-COMMENT: Main-Class will be added automatically by build
Main-Class: excelcomparator.ExcelComparator
I sent the dist folder, which includes the lib folder which contains all the additional .jars.
If there's any other relevant information I need to include, let me know. Thanks!
If you want the JAR to be double-clickable then you should bundle all the dependencies into a single JAR file.
You achieve this using the maven assembly plugin: http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
If you don't know how to use Maven, Eclipse has a nice wizard. I'm not sure how it works on NetBeans but the same question has been asked many times, e.g. here Netbeans Export to Jar, include all library files
Just to finish this off, I'll write what ended up working for me:
As per Franz Becker's suggestion based on this question, I bundled the entire program into a single .jar file.
I was still getting the same error on my coworkers computer, so based on other suggestions I saw, I guessed that the problem was based on different versions of Java installed on our computers. What I did was recompile the program to an earlier version of Java, which works with any version from then and onward (other people suggested reinstalling Java on the users computer. That's a bit too unrealistic in most scenarios).
To change the compliance level (in Eclipse): right click on the project in Package Explorer, select properties. Choose Java Compiler in the options on the left. Uncheck 'Use compliance from execution environment [default target JavaSE] on the 'Java Build Path'', and change 'Compiler compliance level' to your level of choice (I picked the lowest option available, 1.3, as I assumed anything higher than that wouldn't be an issue).
I then exported the file (File -> Properties -> Java -> Runnable JAR File), and had a working, clickable .jar.
Thanks to everyone for their help!
I'm currently using an IDE for writing a java application and testing it. In the IDE, I can run the application and see how it works. However, how would I run the application using a shortcut, or a jar file?
For example, in order to run my WAMP server, I run the wamp.exe file in the WAMP directory. So, I'm running a single file which launches the entire program. How do I achieve this sort of thing with a java application? I've heard about using a jar file, but I'm unsure about whether that would be the proper way to do this or not.
It depends on the IDE you are using. With eclipse for example, you open up the file tab, select export, open java in the tree, and select runnable jar file. Then fill the interface out and your good to go.
If you are using Eclipse, then you can export your application as a single jar file and run it directly by double clicking
In the Package Explorer view:
Right click on your project
Go to "Export"
Click on "Export as Runnable Jar"
And you are done
And if you are using Netbeans, then follow these steps:
Right click on your project
Click on "Clean and Build"
Now got the directory where your netbeans projects are created( usually it should be "C:\Users\your_user_name\Documents\NetBeansProjects"
Open the directory of your project (directory with the name of your project name)
Open "dist" folder and you'll find the jar file of your application/project there.
Java Web Start is the easiest way to add shortcuts for a desktop app.
Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
See also
This answer - the 2 icons on the right "JotPad" & "Star Zoom Animation" are icons installed by JWS.
You can also, just for fun, create a launcher in C++ that launches your executable(the way Limewire did it). A simple console mode C program is given below.
#include <stdlib.h>
int main(int argc, char *argv[]) {
system("java YourProgramClassFileNameHere");
return 0;
}
An advanced methodology would require CreateProcess() in win32API or in JNI for your platform.
You can convert it to an executable Jar file(See Abu's answer)
You can also, create a bat file which can run your program: (Windows only)
#echo off
start java YourProgramClassFileNameHere
Or a shell script(BASH)
Also, see here run a executable jar from c++ code
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Export JAR with Netbeans
I have created a NetBeans Project and now I want to create a package from the project. As this is my first project in NetBeans. Can any one help me to create a package from my project for distribution?
Expanding on the question for the .exe:
I have good experience using Winrun4J to generate an exe launcher for my Java applications. It's quite forward to setup and generate and it supports 32 and 64 bit Windows.
You can even put all needed jars into the exe.
http://winrun4j.sourceforge.net/
But (as you seem to be new to Java): the exe is only a wrapper. It still requires a Java Runtime to be installed on the system where you start the exe!
If you have several project in development environment, make sure you have set the project you want to distribute as a main project. It can be done via mouse right click menu on project name in the left pane.
Click on menu: "Run -> Clean and build main project"
Inspect your project folder, find folder dist in your project folder. There is *.jar file. It is packaged compiled project file.
If you want to code desktop apps. that will run on Windows, OS X & *nix, write them in Java & deploy them from a link using Java Web Start.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
I high-lit the part that is relevant to your next comment.
can I give my own icon image to the executable file?
Why should the user ever see a .jar (or a .exe for that matter)? JWS offers desptop integration, which might consist of a desktop shortcut and/or menu item on supported OS'. Either of those can have an image (within limitations) as supplied by you.
The deployJava.js script checks the user has the required minimum version (helps them to install it, if not), before running an applet or providing a link to a JWS app.
I have created a java application in eclipse, wich needs comm.jar and jexcel.jar and .property files so i have added to libray. I want to make a jar file out of my java appliction, including the external jar files added to the appliction.
How can I do it? To run serialport programs I have copied win32.dll into java_home/bin and comm.jar into java_home/jre/lib and javax.comm.properties into java_home/jre/lib, but when delivering the product it should run only by needing the jre.
How can I solve this? Please help me.
Thanks in advance,
suma
Although your question is not totally clear I suggest using the Fat Jar Plugin should allow you to achieve what you want.
You can use File > Export > Executable Jar which includes all libraries. There is also a checkbox to generate an ant build file as well as the jar in order to customize it further (I for instance make all the paths relative and remove the main-class flag).
You have two "path" issues. The Java Classpath and the path from which dlls are loaded.
If you were using a Java EE app server or OSGi then controlling these paths is addressed by the respective runtimes. Both Java EE and OSGi are likely to be overkill for small projects.
In which case you are delivering:
Your application JAR
The dependent jars
The DLLs
I suggest that on installing your app you place these artefacts into a suitable directory structure, for example .../myapp/lib for the jars and .../myapp/bin for the dlls. Don't copy them into the infrastructure directories, for example the JRE lib and bin, or into Windows32 - that just leads to version nightmares and mysteries when someone installs a new jre.
Having got that structure, how to control the paths? For the classpath, look at the Manifest.mf file. tutorial
For the DLL path, I know of no good alternative to setting OS level environment variables ** before ** launching the JVM. Hence you need a little batch/shell script to launch your app, setting the PATH appropriately.
You can also check maven.
You can right click on the project and say "Export". Now select "Java" in tree of choices. Under that select "Jar File". It'll guide you through the process and will allow you to export you project as a jar file.
Hope thats what you are looking for.
The recently released Eclipse 3.5 has a Export as runnable Jar which allows to put all dependent jars in a subfolder to the jar file, and get the Manifest right.
It is an adaption of the FatJar plugin. Works nicely!