Exporting processing to a jar file - java

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

Related

Running .jar File Java 8

I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.

How to turn folder into jar - Code

Hi im busy on a application that decompiles a jar the pastes files into the folder of the decompiled jar, it then compresses the folder into a jar.
Decompiling and copying works, but i can't manage to get the folders contents to be jared (compressed into jar), i did about 3hrs research and found only outdated methods. please help.
-Regards
marko5049
EDIT MORE INFO:
I apologize i mean i cant get my application to turn a folder into a jar file, my application is an modification installer for a jar file. and it extracts the jars files, then adds the modification and then, is supposed to then turn the folder back into a jar file so that the modification is installed. The jar file is not executable.
This worked for me for a MAC OSX:
Open Terminal at the folder with the jar file and run the following commands
unzip mylib.jar -d jarfolder
//You can then change whatever you need and finally run the command below
jar cvf mylib.jar -C jarfolder/ .
Given that you want to create the JAR through code; you can use JarOutputStream for that. There is an example at this link that contains code to create a JAR file given a File[] containing all the input files.
As for creating the list of files given a starting input path, see Recursively list files in Java.
You could either build a list of files then just use code like in the above example, or you could recursively scan files and add them to the JAR as you go.
If you are using Java 7 and you know your users are too you can also use Files.walkFileTree() with a FileVisitor that adds entries to the JAR as it visits files.
Original answer before OP clarified:
Is there something wrong with:
jar cf my-application.jar folder1 folder2 folder3 etc
The JDK comes with a jar utility to create JAR files.
You can read an official tutorial on it here: Creating JAR Files. It is very straightforward.
If you want to create a runnable JAR, you can create a manifest file that has the main class and other options in it. The linked tutorial describes that process.
The short answer is, ZIP the folder, then rename it to a JAR file.
for windows just make the folder as winrar file.,
to do this right click the folder and click "7 -zip" then
choose "add to foldername.zip".
now a rar file is created with the same folder name.
Then open the cmd in current folder directory
type "mv foldername.zip foldername.jar"
Now you got the executable jar file with your corresponding folder.
The easiest way to make it .. put your folder to C:\Program Files\Java\jdk1.8.0_221\bin and then reach till the same path from CMD then run this
jar cvf Name_your_jar.jar folder
Following command worked for me in Windows 10 and jdk-8u212
jar cf my-application.jar folder1 folder2 folder3 etc
You can put your files in a zip folder. Then convert the zip file into Jar format.A .jar extension file is a Java Archive format file. It is used to store a large amount of files into one single file. You can try a free online file converter without downloading a new software on your computer. There are various online file converters available on Google. I would recommend Convert zip to jar
I hope it helps.
I just found this question and its answers are more useful for your problem:
how to zip a folder itself using java
2 tips :
1、a jar is exactly a zip. So, you just need to zip your folder, and rename it to jar
2、be careful that you should zip your whole folder without changing the relative path of the files, but not just extract all the .class files and zip them together. Because when you run the jar, the class package should be consistent with its path.
I suggest trying to create a regular .ZIP file in Windows.
You need to get 7-zip in order to view the .JAR file you are creating. You should just paste contents into the .ZIP, then rename the file type from .ZIP to .JAR, this worked for me and I hope this works for you.
.JARs are basically .ZIPs created by the Oracle Java client, so you need special file viewing software such as 7-zip or WinRAR to view it for some reason.
You can also revert .JARs to .ZIPs by renaming the file type. You might have to mod your computer with RegeEdit or something to have access to renaming your file types.
I hope this helps.

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.

Java, running the code

I have a .java file that I created in eclipse. I managed to make a .jar out of it, but I don't know how to put my images folder into the jar, nor do I know how to make the jar run?
Should it run already, or would it just be an archive like a .zip?
Thank you guys so much for your help here, because I am completely and totally lost when it comes to this. I have been programming in java (just making .java files in JCreator basically) for about 2 years now.
I have searched and searched but never found a good answer. Anyone that can shed the like on how running .java files as an application and things work, that would be awesome.
P.S. I am using GUI, and the code is correct.
Thank you so much!
What is a runnable jar?
A jar is a archive (it uses the same file format of a ZIP) that contains program artifacts: compiled classes, images, configuration files and other resources.
To be a runnable jar it must contain a special file, called manifest (the manifest support electronic signing, version control, package sealing) with a Main-Class: project.name.EntryPoint entry that specifies the class that is the start point of the program.
This can be easily achieved by using the Eclipse feature File > Export > Java > Runnable JAR file.
How to run a runnable jar?
Using the command line, just do java -jar <jar_file_path>.
But nowadays the graphic shells of the operating systems (Windows Explorer / Gnome / KDE) already call java when you double-click a jar file.
How to put images on the .jar?
You can do this specifying on the Eclipse project that the images folder is also a source folder. Now when exporting the project to the jar file it will include them.

How to make a distributable batch file of my jar

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.

Categories