Create jar file from java file - java

I have a java file with few classes.
I have tried to create jar file but it didn't work. I used eclipse and terminal (mac). For the terminal, I tried to use the command:
jar -cvf jar filename class file class file
I created that jar file but then it didn't work.
Please let me know what is the best way to create jar file.
Thank you

I believe that when you say it didnt work, what you mean is, you couldn't execute the packaged jar file that you created.
A jar file is little more than just a zip of all classes and resource files. You need to add details into META-INF/MANIFEST.MF file so that the class containing the main method to be executed is know.
Alternately you can try invoking the jar file as follows:
java -cp <jarfile.jar> <Complete.Package.ClassNameWithMainMethod>

You did not specify what's not working, but here is the official documentation if it helps any: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html.

Related

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

Creating JAR file from Class file & Jar file

I have a jar file and a class file. I want to create a single JAR file from both. Is it possible? Please help me on the same.
I have tried
Jar -cf new.jar sample1.class sample2.jar
It didnot help
Not sure what you are trying to do, but technically you can do it with:
cp sample2.jar new.jar
jar uvf new.jar sample1.class
Of course, if you already had class sample1.class inside sample2.jar, it will be overwritten ( "u" == update ) by sample1.class
Transfer all your classes into a folder, say it is in C:\Folder\Project\
Go to the same folder from command prompt. And execute the following code :
jar cfv Project.jar *
In the above command, c will create jar, f will transfer the content into the file i.e Project.jar and v will print the process done into the console.
* (WildCard) will take all the files present in the folder and the corresponding jar would be created in the same folder.
You can find more information the DOCS.
http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
In the meanwhile i would suggest you to go through the below link, where you can do the same with a build tool like Ant or Maven.
Clean way to combine multiple jars? Preferably using Ant

Reading file from within JAR not working on windows

I'm trying to read from a file that is packaged up inside a JAR, along with the class that reads it. To do this, I use the following:
getClass().getClassLoader().getResourceAsStream(file)
This works fine when I create and run the JAR file on OSX, but if I create and run the JAR file on windows, the above line returns null.
Am I missing something here? If I create the JAR on OSX and run it on Windows it works fine. The problem only occurs when I create the JAR on windows.
EDIT: It's worth mentioning that there is no folder hierarchy within the JAR file. Everything is stored at one level, thus the class reading the file and the file itself are both in the same directory. Additionally, this is how I'm creating the JAR file, on both OSX and Windows:
jar -cmf manifest.mf run.jar *.class file1 file2
EDIT 2: The file I am trying to load is a java .properties file. I take it that's not what is causing the issue?
Skip the classloader part. Just getClass().getResource....
Try it this way getClass().getResourceAsStream("/file1").
When using file separators, don't hard code them!
Use java.io.File.separator instead: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#separator

Running and Importing files folder along with runnable jar in eclipse

I have a project in eclipse where all my source code is under mbl/src. I am exporting this code as a runnable jar. I have got another folder under mbl/files which contains some of the properties files.I want to export these files also but eclipse doesnt give any such option. I have put the mbl/files under add class folder in build path options but still I am facing similar issues. I have also put the files folder directly under src folder but still it is not able to access it.I am using following options in commandline.
java -jar myprogram.jar arg1
It gives this error:
Unable to find files/text1.properties
text1.properties is under mbl/files/text1.properties
and is referred in the following manner from main class:
FileInputStream fs=new FileInputStream("files\\text1.properties");
Resources in bedded within Jars, need to be read via the Class.getResource(...) method.
This method returns an URL object, from which you can obtain an InputStream

Load files External To The Distribution Jar

I have written a Java program which I package and run from a JAR file. I need to have some user-changeable configuration files which are simply text lines of:
key = value
format. To load these files I used the class described here. When I run my program through Netbeans IDE all works fine as I have included the directory where I store the configuration files in the Project properties.
The problem comes when I build my application into a JAR file. As I want the configuration files to be user-editable I keep them OUTSIDE of the JAR but in the same directory but now when I run my application from the command line it cannot find the configuration files. If I manually add the files to JAR file at the ROOT folder then all is well.
So how can I tell Java to look outside of the JAR for my loadable files? The -classpath option has no effect.
That's because the way you are loading them requires that they be inside the .jar when running from a jar, or inside the project directory if not; it's relying on the classloader to tell it where to find the file.
If you want to open a file outside the .jar, you need to just open it as a File and read it in.
One of the ways we've approached this is to take the external filename as an option on the command line (e.g. java -jar myJar.jar -f filename). This allows you to explicitly state where the file is located. You can then decide whether or not to also look in a default location, or inside the .jar if the file isn't specified on the command line.
I resolved it by referring to this question. I Added the current directory to the MANIFEST file of the jar and it works.
Why is the -classpath option ignored in this case I wonder? Security?
I had the same problem and saw your post, but the answer in the end, was simple.
I have an application deployed via Java Webstart and am building it in Netbeans 7.3.
I have a properties file config.xml that will be updated during run time with user preferences, for instance, "remember my password".
Hence it needs to be external to the jar file.
Netbeans creates a 'dist' folder under the project folder. This folder contains the project jar file and jnlp file. I copied over the config.xml to the dist folder and the properties file was loaded using standard
FileInputStream in = new FileInputStream("config.xml");
testData.loadFromXML(in);
in.close();

Categories

Resources