How to make a distributable batch file of my jar - java

Hi I have created a java application and created a jar file. Then I created a batch file eg-> startup.bat The contents of this file is java -jar MyApp.jar
Now if I double click the batch file then my java application works because both the jar file and batch file belong to same directory. Now I copied the batch file to desktop and run then My application did not run.
Also I want My batch file automatically set the class path.
How to create a batch file with all the above feature
Thanks
Sunil Kumar Sahoo

If the jar is an executable JAR (which it seems to be from your example), you should be able to just double-click the JAR file. If this does not work, you shoud associate the java executable as the program with which to open a file with the .jar extension. See Tools > Folder Options > File Types from an explorer window.
In order that a program be "distributable", it would normally be packaged (i.e. the JAR file and startup scripts) in some ZIP archive and then extracted to a folder on a user's PC. Your startup script should provide a relative path to the JAR file. For example if you have organized the contents of your ZIP into lib and bin folders:
MyProject
+--- lib (contains jars)
+--- bin (contains .bat script)
In this case, the startup.bat file should reference the Jar as:
%JAVA_HOME\bin\java -jar ..\lib\myapp.jar
This requires that any users:
Have a JRE installed on their system (unless you package that up as well)
Have java on their path or a JAVA_HOME environment variable set

The problem is that the batch file doesn't know where the jar is, it is looking relatively from its own path. You could copy the jar file to your desktop, too.
If just want an icon on your desktop, make a shortcut to the batch file rather than a copy.

Aslo if you application is quite complex (size more than 1+GByte) - you can distribute it with jre included.

Because it's a bit long to copy in here, I'd suggest that you have a look at what Squirrel SQL does. And you'll have a very nice cross-db tool as an extra bonus ;-)
They enable the tool to be started in two possible ways:
by starting the jar itself, classpath info will then be fetched from the manifest file (see here for the details, look up download extensions). In that case, after unzipping the hierarchy of files as explained by other posters, just make a shortcut on the desktop and you're all set
with a batch file. You'll see that the batchfile solution is quite complicated, especially the construction of the classpath.
So I'd go with the first solution.

I understand that you would like to distribute your application to multiple machines? A (very) simple solution would be to create a zip/tar/whatever archive that has all the needed jars (including MyApp.jar) and the startup script.
In the script you can use the -cp option to set the classpath to the jars:
java -cp needed.jar:needed2.jar -jar MyApp.jar
As said above, making a shortcut to the desktop is a better option than copying the script itself.

Related

Exporting processing to a jar file

I've been working on a processing application using ControlP5 and Twitter4j. I want to have my project run from a single jar file from any operating system. Basically I want to package up my application. My application uses images. I've been browsing for more than an hour, but I cant find how to do this. Any suggestions?
using
processing 2
twitter4j3
Thanks in advance!
I dont know if you can directly do it from the Processing IDE however, if export your sketch to a Java applet then locate the .java the the sketch folder you can use this in conjunction with Eclipse to export to a jar file.
So, I know that this post is very old but if you are still looking for a solution, or to other people that see this thread, it's relatively simple.
Export the project
In the folder with the exported project (something like application.windows64), navigate to lib and find core.jar and project name.jar (you need to have file name extensions visible)
Rename the files to .zip files
Extract core.jar to whatever folder
Extract project name.jar into the same folder (make sure you don't do it into a subfolder)
Click yes if it asks if it wants you to replace a file (if it doesn't you extracted the files incorrectly)
Delete core.jar and project name.jar
If the project uses images, move them into the same folder as all the other files
Select all of the files in the folder, right click, hover over send to and select compressed (zipped) folder
Rename the .zip file to name of project.jar
This might be old, but i still find other posts about it on processing forums.
This is the best way to run processing project as a jar file.
When exporting application, you will always end up with a lib folder inside exported application(whether for Linux and Windows). For windows, open command prompt(or power shell), you can use right-click+shift and then click on open power shell here.
After that you can run the following command.
java -classpath lib\* DisplayDepthStream
Now DisplayDepthStream is the name of sketch file.
To explain the command, -classpath lib\* tells java to add everything under lib directory to the class path. And DisplayDepthStream is the name of my main class.
Hope this helps~!
Chears

Creating an executable installer in Java

I am interested in making a one-click installer for my C# application.
I had the the framework of the application down. The logic of the application in the installer() method was:
public static void installer(){
deleteLegacyFiles(); // deletes old files through a find method
moveSQLite(); // moves the database file
if(checkRevit2013()){ // checks whether Revit '13 is installed
movePlugin2013(); // moves my plugin into the Addin folder or Revit
}else if(checkRevit2014()){ // check whether Revit '14 is installed
movePlugin2014(); // moves my plugin into the Addin folder or Revit
}else{
System.out.println("It does not look like you have either Revit 2013 or Revit 2014 installed.");
}
}
However, this Java script (not Javascript, but a Java script) really only took three folders from the /Desktop/ and copies them to their respective target folders. I am interested in a solution that turns all my three folders into one executable file (something like an .exe or .msi) and do the above actions.
Are there any solutions for this for Java? Something that packages multiple folders/files together and then allows for one-click solutions for installation? I'm not exactly how to phrase what I want, as this is my first software development project. Any suggestions are welcome.
You can create a single executable jar file in java. This jar would have an application that does all the copying you've listed above. But instead of copying from the desktop, it would copy directories that are included in the executable jar. A jar is a zipped file type (in fact you can change the extension from jar to zip and examine the contents).
Your strategy will be to create a regular java application, package as an executable jar. Include the directories you want to install as resources in the jar. Check out the jar documentation for all java utility methods and classes to manipulate jars.
http://docs.oracle.com/javase/tutorial/deployment/jar/
Are you looking for making/building an executable jar file? If so you can use something like one-jar.
http://one-jar.sourceforge.net/index.php?page=introduction&file=intro
Here are the steps:
Create an executable JAR file with your application's CLASS files in it. (Navigate to bin directory of workspace) Name this "main.jar"
jar cfm main.jar manifest.txt *.class OR [jar cfm main.jar manifest.txt .]
Create three directories: MAIN, LIB, and BOOT
Place your "main.jar" file in the MAIN directory.
Place the jar files that your main application depends on in the LIB directory.
Naigate to Packaging- Create a new JAR file out of the MAIN and LIB directories. Name this one "MyUtil.jar". You do not need to add a manifest or do anything special to this file. It does not need to be executable. Just make it so that it contains the contents of the MAIN and LIB directories.
jar cf MyUtil.jar main lib
Extract the contents of the "one-jar-boot.jar" file into the BOOT directory.
Navigate to the BOOT directory, and update the "MyUtil.jar" file with the following:
jar -uvfm ../MyUtil.jar boot-manifest.mf .
Your "MyUtil.jar" file should now be executable. Test it.

How to get location of runnable .jar File when run from GNOME

I am developing a small Java application using Swing, that is supposed to run on Windows/Linux and MacOS.
Eventually it will ship as a runnable jar with some config files in the same folder. To load them I need the path to the folder the jar is stored in within the program.
There are already a couple of threads like this one or this one.
My problem is, that all the solutions discussed there work fine, when I run the program from within eclipse or call the runnable jar from a terminal like so:
java -jar /path/to/jar/jarfile.jar
However when I click on the jar file in Cinnamon or Gnome (which is what most of the users will know to do), I do not get the correct paths. (MacOS users report the same issue)
Here is what I've tried so far and what the output is when run via double click (all those display the desired path when run from eclipse or a terminal):
ClassLoader.getSystemClassLoader().getResource(".").getPath()
Output: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Output: ./
System.getProperty("user.dir");
Output: /home/myusername
Is there any other way to do it or am I doing something wrong when exporting the jar? Any help would be appreciated!
Cheers
Nick
Make it simple, and use a startup script (.bat/.sh file) to run your application. This startup script will get the path of its own location in the filesystem, and pass it as an argument or system property to the Java application. This has the additional advantage of being able to pass other arguments, like the size of the heap, etc.
On windows, %~dp0 is the path of the directory containing the executed bat file. On Unix, you can use $(dirname $0).
You could store all config files as resources in a jar, and copy them to files in home.dir + ".AppName/".
Painful as it is, the Preferences API, Preferences.systemNodeForPackage, seems the wisest alternative, if there is little structured config data. There is an inputStream method for import; your initial config template could be a resource in the jar.
Just get the class path using System.getProperty("java.class.path") and scan it for your ".jar" name. Note that path separators are OS dependent (File.pathSeparator)

making .exe file for java project containing sqlite

I want to create .exe file for my java project and give it to my friend. I wrote my project in eclipse and it uses sqlite. I don't know how to make a .exe file that can be run in other PCs.
Can any one help?
P.S:I saw this link but it is not useful for me!
Create .jar of an eclipse project that uses Sqlite DB
.exe is a creature of Windows.
To run a Java program, you typically:
Create a .jar file (the "native" Java library format)
Write a DOS/Windows .bat file (or, equivalently, Linux shell script) to run your Java program (using the Java .jar file)
Optionally, create some easy-to-use mechanism for the end user to download the Java JRE (if it's not already installed on their PC).
Your .bat file can be as simple as this:
start javaw -jar myjarfile.jar
Have you considered creating Runnable jar from eclipse.
In eclipse, go to File > Export > Java > Runnable Jar.
There you ll find some options and you can use what suits you. The jar created should be able to run all by itself (obviously it needs the java run time).
Try this out.
I would recommend using a bat file. You can make a double clickable jar file, but I feel that is sometimes restrictive and not intuitive.
Not many end-users know that a jar file is double clickable.
You need to make sure the jar file has a main class and classpath defined. The classpath section in the jar file sometimes causes issues. Like you cannot reference a file or path on the file system. Only files or folders that can be relatively referenced from the location of the jar file.
For windows users, you cannot easily make an exe file from a jar file. There are methods like using jsmooth, that will wrap your jar file into an exe file (bloating the exe file in the process).
The easiest way is to create a bat file. You can easily convert a bat file into an exe and make the exe file have an icon and everything. Link to a converter here:
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html
First create an executable jar file by clicking on File menu, then export, and then select runnable jar file.
Then select main class and click ok - the jar file will be created.
After that use Launch4j application to create .exe. Launch4j is the best option for creating an exe file.
Then use Inno Setup Creater to create an installer and it is done.

How can i create executable apple .app file from my java .jar file?

I have created an executable java Swing .jar application. It works fine on Windows. The application hierarchy is :
application.jar
images(Folder) .......... Contains all images the application uses.
libraries(Folder) ....... Contains all external jar libraries the application uses.
bundles(Folder) ......... Contains all bundle files the application uses.
database(Folder) ........ Contains the database files the application uses.
All the above folders exist outside the jar file. Now i am trying to create a Mac executable file (.app) from "application.jar" to run it on Mac so i used the "Jar Bundler" as specified here but when i run the output application.app file nothing happens, nothing runs and i can't even debug it.
I think the main reason is that it can't see the external folders. So is it impossible to create a .app file if the application has external folders ?
And is there a way to debug the .app file to see what's going on ?
Nothing runs, and i can't even debug it.
Diagnostic output from the launch process may be obtained as follows:
$ export JAVA_LAUNCHER_VERBOSE
$ ./YourApplication.app/Contents/MacOS/JavaApplicationStub
There's a related example here.
Most likely the problem is your working directory.
When you run an executable JAR file by double-clicking it, the working directory is the parent directory of the JAR file.
By default, the working directory of an application bundle is its parent directory. If you package the external folders into the application bundle they will be located under $APP_PACKAGE/Contents/Resources.
So the assumption about the working directory that you make for an executable JAR file does not hold for an application bundle.
In order to set the working directory to the resources directory, add
<key>WorkingDirectory</key>
<string>$APP_PACKAGE/Contents/Resources</string>
to the Info.plist file of your bundle.
In case you know nothing about application bundles, please read this document.
This might help: AppBundler by Josh Marinacci
I am not sure about your exact directory hierarchy. But on a Mac with Xcode installed is an application called "Jar Bundler". It exist for exact that purpose you are asking for.
BTW, Mac application use the suffix .app, that is right. But they are not files. Thery are directories.

Categories