This question already has answers here:
How do I make a JAR from a .java file?
(8 answers)
Closed 6 years ago.
I just have some (134) .java source files with me and I'm pretty sure that contains all the necessary code!
I want some quick tutorial to compile this program into a .jar file.
(I'm on a windows platform btw)
I have tried javac and jar commands. I got my .jar file but it's not opening!!!
Thanks in advance!
Best practice is to make use of Ant/Maven/Gradle kind of build program tools that can take care of creating Jar files.
Other way is to just make use of Eclipse's feature for exporting the project as a light weight Jar file or Runnable Jar file(which includes dependencies).
Place all the files you want to include in the JAR file inside a
single folder.
Open the command prompt in Admin Mode
Navigate to the folder where you stored your files.
Set the path to the directory of the JDK bin. You will need to run
the jar.exe utility to create a JAR file, and that file is located
in the bin directory.
Create the JAR file.
The format of the command line for creating the
JAR file looks like this: jar cf 'jar-file'.jar input-file(s)
You can use WINRAR.
right click on file (put inside all your .java files) and compile by using winrar;
choose format .zip (important)
and save filename.jar (important)
Related
I have a program that i wrote with 3 java files but it uses images and text files in other folders, is there any way to put it all in any jar file or exe or something similar? Im using dr java. this is what it looks like now screeenshot
You can compile them to classes and them join them to one (executable) JAR file as described here: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Edit: Eclipse has a feature to export (right click on project/Export.../JAR) to a jar file right away.
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
I'm writing a MP3 player in Java.
If i will finish I want to pack all .class files in one .jar file. I don't want have player, which starts by console.
If i open this one .jar file i want see player window.
I know how to pack it. I must use jar.exe packer with params: cvfm Player.jar MANIFEST.MF ./config/*.class and create MANIEST.MF which content class that has main method.
But the problem is when I want use another Look and Feel, or use existing .jar file. I can put this existing .jar file into my player main dir and compile javac.exe with parameter -cp .;./JarFile.jar, but when I pack all compiled .class files and my existsing JTattooDebug.jar file into one Player.jar file i don't see new Look and Feel i just see default view.
If you are using eclipse.
Right-Click your project
Export -> Runnable Jar File
Then select the destination for your jar, and make sure you have "Extract required libraries into generated JAR" selected. That will give you a runnable jar, complete with auto generated manifest, with all of your needed jars inside.
Solved! I had to add line:
Class-Path: lib/JTattooDebug.jar
in my MANIFEST.MF file, now all works.
This question already has answers here:
How can an app use files inside the JAR for read and write?
(5 answers)
Closed 8 years ago.
Is it possible to create or modify files inside jar and if it is how to accomplish that?
One possible solution is to unzip the jar, modify it and zip it back up.
NOTE: You can extract it with a utility like 7-zip or WinRar.
You can use jar command if you would like to do it through command prompt (or) you may use winzip (or) winrar tools.
Example:
jar uf foo.jar -C classes . -C bin xyz.class
Ok here are two ways to do this:
Use your favorite zip application, unzip it, change the files and zip it again.
Use a zip library like Zip4J and do the same in your java application.
In a word, yes its possible and you have several options:
Modify the existing jar file. Do this by extracting the jar file (use whatever extractor you need to), modify the file you want (decompile if you don't have the source) and then repackage the jar.
If you don't want to go thru the trouble of changing the jar, you want to copy the existing class file (again decompile if you don't have the source), make the changes you want, compile it and make sure your class file is ahead of the jar in your CLASSPATH. That way when your VM does its classloading, it'll get your modified class first and ignore the one in the 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();