Creating & Using .jar files and manifests
I've read through hundreds of online tutorials on creating and using .jar's, but it's surprising how unhelpful they really are.
I've been trying to make a java program of mine standalone, so I can distribute it to others and allow them to run it without use of a compiler, etc (ideally just a .exe or .bat that they double click on). Everything I've found says I need to create a .jar of my program in order to do this. I was finally able to create a .jar but I got different errors relating to "main-class" and "manifest" issues...I tried running it with javaw.exe and had the same problem.
Some tutorials briefly discuss "meta manifest's", other's skip the concept all together in creating .jar's. I created a jar with:
jar cf A2.jar c:\jarTester.java
after changing my command prompt path to
C:\Program Files\Java\jdk1.6.0_01\bin
It created the .jar, but nothing worked. As above, it gave me errors relating to the "Main-Class" and "Manifest". So here's my question:
What involvement does a "manifest" play in .jar's, and how do I create/use one to create a .jar? Why is it necessary to specify the "Main-Class" when I have already done so in my .java file?
I believe that once I can get this "Main-Class" and "Manifest" problem straightened out, I will be able to create a batch file with the line:
java -jar A2.jar
is this correct? THANKS.
I've read through hundreds of online tutorials on
creating and using .jar's, but it's surprising how
unhelpful they really are.really, hundreds?
I've been trying to make a java program of mine
standalone, so I can distribute it to others and
allow them to run it without use of a compiler, etc
(ideally just a .exe or .bat that they double click
on). Everything I've found says I need to create a
.jar of my program in order to do this. Make up your mind, do you want to make a jar or an exe?
I was
finally able to create a .jar but I got different
errors relating to "main-class" and "manifest"
issues...I tried running it with javaw.exe and had
the same problem.
Some tutorials briefly discuss "meta manifest's",
other's skip the concept all togetherthat's because the manifest isn't usually necessary
in creating
.jar's. I created a jar with:
jar cf A2.jar c:\jarTester.javaugh, that won't work. you need to put the class file in there, not the source. or you can put the source in as well if you want, but the class is necessary
i'm ignoring the rest of this post for now
go back to Google and type in jar manifest and read more
Similar Messages
-
Executable jar files and manifest file
Hi,
I have the following files in a folder named: Test
a.jar
b.jar
c.jar
Driver.class
Driver.mf
Here, MainClass is the main executable class that uses a,band c.jar files. This is how my MainClass.mf file looks:
Manifest-Version: 1.0
Main-Class: Driver
Class-Path: a.jar b.jar c.jar
Now I use the following command to make one executable jar file:
jar cmf Driver.mf DriverMain.jar *
It creates an executable jar file named : DriverMain.jar
Now I copy the executable jar file (DriverMain.jar) into a different folder named: RunTest
and double click it, doesn't work.
my question is:
what am I doing wrong? Any special characters needed in my Driver.mf file (space/newline/etc)?
What I am trying to get is: One execuatble jar file so I can just double click to run it, and that single executable jar file will have all the necessary jars in it (i.e. a.jar, b.jar, c.jar in this case)
Anyone please help!
Thanks
-RonRony,
Sorry to disappoint, but you can not use embeded jar/zip files within an executable jar. The JDK sadly for some reason decided this was not a useful idea, so developers like me who want to distribute plugins with thier own dependencies have one of two choices. You either have to unzip the jar file that contains the embeded jars to a directory, then run your primary exectuable jar, OR you have to write a custom classloader that your "launcher" creates then loads the embeded jar files.
The best thing to do is either jar up a, b, c and driver into one jar, so that it works, if you can legally do this. A lot of 3rd party libraries may not allow this per their license. Otherwise, another choice is to use a free installer or buy an installer that allows you to distribute a single exectuable installer program that will properly create the dir structure you need.
As for the way it works, if you declare:
Class-Path: a.jar b.jar c.jar
the JAR loader code in the JDK looks in the root dir where your application was started for a/b/c jar files. They have to be on disk.
If you want to place them in /lib, for example, you would havd a jar file like:
Driver.class
Driver.mf
lib/a.jar
lib/b.jar
lib/c.jar
When unzipped, your "root" dir would look exactly like the above, and your manifest would have Class-Path using ./lib/a.jar ./lib/b.jar ./lib/c.jar
Anyway, there thus far isn't any way around this issue of embeding jar files in an executable jar file. You MIGHT be able to not specify any classpath, then in your Driver.class, create a new custom classloader that dervies from URLClassLoader, but in the findClass(), you get a ref to the .jar file that the classloader class is inside of, and from that use it to find the lib/*.jar files and add them to the classpath. For this to work, however, your Driver.class code should ALSO be contained in an embeded jar file that is loaded by this custom loader. The only thing Driver.class would contain (and I would rename it to something like Launcher) is the code to create the custom classloader.
It's fun stuff, but a little bit of work to make it work. You can infact make it work! I may yet one day take our plugin engine custom loader and create a way for this to work! -
How do i making a JAR file manifest using SJSE8?
The J2EE project support also has automatic generation of manifest for specific entries such as Class-Path.
-
My applet class is using other classes in my package,
So do i need to create a jar file and specify that jar file in <applet archieve = :myjar.jar"
I did created the jar file , but its not loading up, the browser just says Loading java applet, what might be the reason, does it depends on size of jar file?
or my tags are wrong.
my applet tag is
<html>
<applet archive = "myjar.jar"
code = "front.class"
width.. >
the front.class file is also in myjar.jar.
Any help....
Also one more question,
Can i call other applets within one applet class.
I have a gui, when a particular button is clicked on applet
i want to go to other applet, so how should i call it in that buttons action listner?
And in my applet class i am using one other class from my package which actually coonects to databse through JDBC. now say i have a button on my applet whihc says "Connect", and when that button is clicked then I created object of my other class which makes connection
through databse. So will this work from applet?
Thanksmy applet tag is
<html>
<applet archive = "myjar.jar"
code = "front.class"
width.. >Just to get you started - if it is a JApplet, not an Applet, then you need to use different HTML tags, otherwise it won't work. If you have JDK1.3, look in the /bin directory - you should see a file called "HTMLConverter.bat" if it's there, cd to the directory where your HTML file is, and type
"HTMLConverter wateverYourHTMLPageIsCalled.htm"
(case sensitive - and remember to check whether its called ".htm" or ".html")
When it has executed successfully, look at the source of your html - it should be different (<OBJECT> tags and lots of other stuff)
If you have an older version of the JDK, or don't have HTMLConverter.bat, you can download it from Sun's website.
Now you should be able to run your applet...
>
Can i call other applets within one applet class.
If you use "getAppletContext().showDocument(URL url)", this will replace the current html page with the new one - is that what you mean?
And in my applet class i am using one other class from my package which actually
coonects to databse through JDBC. now say i have a button on my applet
whihc says "Connect", and when that button is clicked then I created object
of my other class which makes connectionthrough databse.
So will this work from applet?Should do :-) -
My applet class is using other classes in my package,
So do i need to create a jar file and specify that jar file in <applet archieve = :myjar.jar"
I did created the jar file , but its not loading up, the browser just says Loading java applet, what might be the reason, does it depends on size of jar file?
or my tags are wrong.
my applet tag is
<html>
<applet archive = "myjar.jar"
code = "front.class"
width.. >
the front.class file is also in myjar.jar.
Any help....
Also one more question,
Can i call other applets within one applet class.
I have a gui, when a particular button is clicked on applet
i want to go to other applet, so how should i call it in that buttons action listner?
And in my applet class i am using one other class from my package which actually coonects to databse through JDBC. now say i have a button on my applet whihc says "Connect", and when that button is clicked then I created object of my other class which makes connection
through databse. So will this work from applet?
ThanksYou didn't show all of your applet tag, but it should look like this:
<HTML>
<APPLET CODE = front.class, ARCHIVE = myjar.jar, width = ???, height = ???>
</APPLET>
</HTML>
Assuming your applet is not crashing, this should load it. I recommend using the appletviewer tool in the JDK, it provides more detailed error messages. I hope this works for you.
Now inter-applet communication. This is forbidden, sorry. However, if you really want to get fancy; remember that all applets can communicate back to the host from which they came. So if you want to build a small server on your host machine which all your applets log into, they can then communicate with each other through the server. -
Performance issue if we use jar file instead of classes
Hi,
My application uses tomcat as web server.
If i use calsses in webapps -> WEB-INF -> classes folder, i place classes in that ,
In other case i use jar file and place that file in WEB-INF -> lib folder in the webapps directory.
There is huge performance difference.
While using classes performance is great while using jar file performance is very disappointed.
I am using a file for encryption /decryption also.I can't really believe that classes vs jars makes a difference, but whatever.
-
Encryption/decryption through jar file and classes
Hi,
My application uses tomcat as web server.
I am doing encrytion and decyption.
i fetch encypted data from database and then decrypt it
If i use calsses in webapps -> WEB-INF -> classes folder, i place classes in that ,
In other case i use jar file and place that file in WEB-INF -> lib folder in the webapps directory.
There is huge performance difference.
While using classes performance is great while using jar file performance is very disappointed.
I am using a file for encryption /decryption also.Are you getting any error messages? Have you put debugging code in those classes to see what is happening?
-
Problem in Creating a jar file using java.util.jar and deploying in jboss 4
Dear Techies,
I am facing this peculiar problem. I am creating a jar file programmatically using java.util.jar api. The jar file is created but Jboss AS is unable to deploy this jar file. I have also tested that my created jar file contains the same files. When I create a jar file from the command using jar -cvf command, Jboss is able to deploy. I am sending the code , please review it and let me know the problem. I badly require your help. I am unable to proceeed in this regard. Please help me.
package com.rrs.corona.solutionsacceleratorstudio.solutionadapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import com.rrs.corona.solutionsacceleratorstudio.SASConstants;
* @author Piku Mishra
public class JarCreation
* File object
File file;
* JarOutputStream object to create a jar file
JarOutputStream jarOutput ;
* File of the generated jar file
String jarFileName = "rrs.jar";
*To create a Manifest.mf file
Manifest manifest = null;
//Attributes atr = null;
* Default Constructor to specify the path and
* name of the jar file
* @param destnPath of type String denoting the path of the generated jar file
public JarCreation(String destnPath)
{//This constructor initializes the destination path and file name of the jar file
try
manifest = new Manifest();
jarOutput = new JarOutputStream(new FileOutputStream(destnPath+"/"+jarFileName),manifest);
catch(Exception e)
e.printStackTrace();
public JarCreation()
* This method is used to obtain the list of files present in a
* directory
* @param path of type String specifying the path of directory containing the files
* @return the list of files from a particular directory
public File[] getFiles(String path)
{//This method is used to obtain the list of files in a directory
try
file = new File(path);
catch(Exception e)
e.printStackTrace();
return file.listFiles();
* This method is used to create a jar file from a directory
* @param path of type String specifying the directory to make jar
public void createJar(String path)
{//This method is used to create a jar file from
// a directory. If the directory contains several nested directory
//it will work.
try
byte[] buff = new byte[2048];
File[] fileList = getFiles(path);
for(int i=0;i<fileList.length;i++)
if(fileList.isDirectory())
createJar(fileList[i].getAbsolutePath());//Recusive method to get the files
else
FileInputStream fin = new FileInputStream(fileList[i]);
String temp = fileList[i].getAbsolutePath();
String subTemp = temp.substring(temp.indexOf("bin")+4,temp.length());
// System.out.println( subTemp+":"+fin.getChannel().size());
jarOutput.putNextEntry(new JarEntry(subTemp));
int len ;
while((len=fin.read(buff))>0)
jarOutput.write(buff,0,len);
fin.close();
catch( Exception e )
e.printStackTrace();
* Method used to close the object for JarOutputStream
public void close()
{//This method is used to close the
//JarOutputStream
try
jarOutput.flush();
jarOutput.close();
catch(Exception e)
e.printStackTrace();
public static void main( String[] args )
JarCreation jarCreate = new JarCreation("destnation path where jar file will be created /");
jarCreate.createJar("put your source directory");
jarCreate.close();Hi,
I have gone through your code and the problem is that when you create jar it takes a complete path address (which is called using getAbsolutePath ) (when you extract you see the path; C:\..\...\..\ )
You need to truncate this complete path and take only the path address where your files are stored and the problem must be solved. -
Create a jar file using the jar tool
Hello all,
can somebody help me with this situation:
I use the following comand
jar cfm Seti.jar manifest.mf SetiSeti.jar is the name of the file I want to create and Seti is the name of the folder where I have the class files, the manifest file and the Seti.java. The manifest file is also out of this folder (exactly the folder were I use the command on the command line).
After creating the jar... I try to run it but it gives an error message: "can't find the main class..."
The content of the manifest file is:
"Manifest-Version: 1.0
Main-Class: Seti
I'd never created a jar file before and have already read all the usefull information here and at sun... I'm very preoccupied with this..
Can you help?
ThanksRuiAranhaJava wrote:
How do I do this?
"And you have to name that package as well when you name the main class in the manifest. "
But, as I have told the name of the package is SetiNo, you just told us that the name of your class is Set. You never told us about the name of the package.
Simples question possible: Does your class look something like this:
package Seti;
// maybe there are some import statements here, maybe not
public class Seti {
// maybe there's something here, maybe not
public static void main(String[] args) {
// anything
}If so, then the fully qualified name of your class is "Seti.Seti" and you need to write that into the Main-Class attribute. (*)
If there is no package-Statement, then the fully qualified name of your class is simply "Seti" and the Main-Class attribute is correct, but your classes are in the wrong place.
Edit:
(*) Please note that package names should generally be all-lowercase, so it should be "seti" instead of "Seti". This helps distinguish it from the simple names of classes, so it's easy to see what you are talking about at a glance: "seti" would be the package, "Seti" the class name. -
Using JAR file in running application and POI !
Hi all
I am trying to use POI HSSF f(for the first time, So pls bear with me!)or creating excel file from my java Application. I am using the jar file :
poi-2.0-RC2-20040102.jar. Not sure if its the correct one. So If I am wrong pls correct me.
Now I am creating a bat file to compile my application:
@echo off
C:\jdk1.3.1_04\bin\javac -classpath C:\POI\poi-2.0-RC2-20040102.jar MyApp.java
pauseIt compiles fine. Now If I try to use the jar file in run.bat for running my application, it says NoClassDef FoundError:MyApp. Here is my run.bat
java -classpath C:\POI\poi-2.0-RC2-20040102.jar MyApp
pauseI am not able to run my application with this run.bat file . If I change the content to:
"java MyApp.java"
It runs but give me error: NOClassDefFoundError:import org.apache.poi.hssf.usermodel.HSSFWorkbook
Pls suggest me about how to use jar file in running my app and how to rresolve workbook finding problem.
ReagrdsTry this:
java -classpath .;C:\POI\poi-2.0-RC2-20040102.jar MyApp
pauseNote the "dot semi-colon" at the start of the CLASSPATH. That puts the current directory in the CLASSPATH, which is where MyApp.class lives. (No package, right?)
See if that works better. -
Help with creating jar file and running it ?
Hey guys,
I have a program with a package called classes and a sub-package called classes.mainLib.
I am trying to create a JAR file of the class files in mainLib. There is only one class file in classes and that contains the main method. So this is what i am doing:
1. create a manifest file with: Main-Class: classes.Cars
2. from within the mainLib directory (./program_name/classes/mainLib/) i do this:
jar cvf mainLib.jar manifest ./*.class
3. Then i move this jar file to ./program_name/lib and change directory to ./program name/
4. try to do this: java -jar ./lib/mainLib.jar
But it errors with:
gary@linuxbox:~/java/cars$ java -jar ./lib/mainLib.jar
Failed to load Main-Class manifest attribute from
./lib/mainLib.jar
Any ideas why this is happening. the manifest is included in the jar'ing
Thanks.At a glance, it looks like you are placing only the
class files in the jar and trying to navigate your
classpath to run it. Instead, you have to place ALL
the package folders into the jar from the root of
your package hierarchy and the jar runs anywhere, in
any folder. The packages are INSIDE the jar.
also I see cars/classes in a path you describe ...and
the program seems to wnat classes/Cars ??? Could
this be an additional issue (or am I missing seeing
something about your package structure)?Firstly, i believe all my spelling and cases are correct.
I have the following strcuture:
Cars.class (contains main) : package classes in ./classes directory
All other classes : package classes.mainLib in ./classes/mainLib
I would like to jar ALL files in mainLib but not the class that contains the main method. Is this possible?
I'm not sure how clear i was earlier, hope this is more understandable. -
Zip or Jar files and how to use them
Hi ,
I have this problem. I have some .class that must be
packaged some way to deploy in the application user's directory. At some point of the execution the application will ask to create some object define in one of that classes I mentioned. So how can I package those classes, in a jar file or in a zip file? And most important will it work out ?!.
Many thanks in advance.Using JAR Files: The Basics
-
Create a jar file programmatically using runtime.getruntime??
try
String s[]={"cmd.exe","/c","C:\\programfiles\\java\\jdk1.6.0\\bin\\jar.exe","-cfm","jarname.jar","manifest.mft","classes.class"};
Runtime.getRuntime().exec(s);
catch(Exception er)
}i want to ask, why this code doestn work, i want to create a jar file using the runtime.getruntime.exec command..
please correct me, thanks
is it possible??
thanks,Probably because the working directory is not where you expect so does not contain the 'classes' directory or the manifest file. You can define the working directory by using the exec() command that has 3 arguments (setting the second to null) or you can use the full paths to the files/directories.
If you have no done so then you should certainly read
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
Edit: I just looked again at your post - 'classes.class' seems a funny name for your class file or for the root directory for your class files. Have you tested the 'jar' command from the command line? if not then do so.
Edited by: sabre150 on Jan 8, 2008 9:54 AM -
Create BPEL jar file using Bpelc via Java classes
HI,
I am trying to create the BPEL files ( xyz.bpel, bpel.xml, xyz.wsdl etc.. ) on the fly using Java code... Once I create all these files, I create a packaged jar (Ex : bpel_xyz_v2006_10_17__37256.jar) file and deploy the same in the Bpel PM.
Right now, in order to create the jar file, I am running the bpelc.bat file under bpel/bin and then using the IBPELDomainHandle, I am deploying the process.
But my requirement is to create the jar file using java rather than executing the bpelc.bat file..
Can you please give me pointers as to how to achieve the same?
Thanks
PramodActually, I had figured out the part of calling the Bpelc class, but initially I was trying to create an object of the class and was not able to do so. That was where I got stuck.
Eventually, I did something like the code snippet below and it works fine and the jar file is created. Just fyi for anyone looking in the future.
String[] setupValues;
setupValues = new String[]{ "-home", "D:\\product\\10.1.3.1\\OracleAS_1\\bpel", "-rev",
"1.0", };
Bpelc.main(setupValues);
Thanks
Pramod -
Music and Video on Nas in folders but how do i set up itunes on multiple computers to see and use these files and create libraries?
So i have had a itunes set up on my old PC, bought a NAS and copied the folders over to the NAS, i did this incorrectly and so then even when i told the old PC to use that folder it saw all the songs but wasnt able to play the songs as it was looking in the incorrect place.
So now i want my Mac as well as my PC and others to all use the music, videos etc on the NAS they are in itunes friendly folders (as they were compiled this way by the itunes on the old PC.
When i tell the mac to use the itunes library.itl file it sees the song list (about 100gb) but cant see any songs, so i have removed this file to another location for now with the hope to set up a new file and then get it to see the songs on the folder from the NAS.
Can someone tell me how to do this for all the Mac's and PC's on my network as i really want one master library that all use and add too.
Thanks for your help in advance.I have the same question but I am using two pc's
Maybe you are looking for
-
Error Key word when interfacing EPMA interface table
Hi, I'm trying to interface a SQL server database with EPMA interface tables. So basically, I'm doing SQL server to SQL server. I already have several Interfaces that work fine, but I encounter an issue when interfacing a ...PropertyArray table in EP
-
Sun app server problem using asadmin tool
I have installed sun app server and trying to do the simplemessage tutorial. When I run the ant create-cfcommand I get the following [exec] Invalid user or password [exec] CLI137 Command create-jms-resource failed.I have put an echo line in app-serve
-
Hi, I'm currently trying to teach myself how to use flash CS5 and actionscript 3 and apart from doing small amounts of programing here and there I'm a complete novice. I have 36 photographs taken at 10-degree angles, making an object and i have each
-
Directly creating text elements from eclipse
Hi, I just came into the situation, that I'd like to translate some strings in my source code. The fastest solution in SAP GUI was to just double click on the string and create the corresponding text element. In eclipse I can navigate into existing t
-
How can i verify my account to start using my iphone 5 Im waiting for an wmail but still... NO EMAILS FEELING STRESSED AND DISSAPOINTED :-(