Include a class file into a JAR file - java

Can anyone please tell me how to add a class file into particular package inside a JAR file using command prompt.
Example: Test.jar has a packaging structure com.test
Now I want to add a class file called Test.class into com.test package of Test.jar file.
Your help will be highly appreciated.

You can update a JAR file by passing the 'u' argument and supplying the JAR and file you want to add:
jar uf Test.jar com/test/Test.class
More info about this option here.

jar uf jar-file input-file(s)
In this command:
The u option indicates that you want to update an existing JAR file.
The f option indicates that the JAR file to update is specified on the command line.
jar-file is the existing JAR file that's to be updated.
input-file(s) is a space-delimited list of one or more files that you want to add to the Jar file.
Any files already in the archive having the same pathname as a file being added will be overwritten.
Ref : http://docs.oracle.com/javase/tutorial/deployment/jar/update.html

Related

How to get java main class from jar file

I have an executable jar and i want to know the name of java main class from this jar
My question are if there is a java cmd to get this from jar metadata ?
You can use unix command :
unzip -p XXX.jar META-INF/MANIFEST.MF
or you can use jar commad
jar tf XXX.jar and see the content of MANIFEST.MF.
Technically a jar file can contain more than one main class.
When java executes a jar file, it looks in the META-INF/MANIFEST.MF file inside the jar to find the entrypoint.
There is no direct command to get this information, but you can unpack the jar (it's just a zip file) and look into the manifest yourself.
More about the manifest: https://docs.oracle.com/javase/tutorial/deployment/jar/defman.html
More about application entrypoints: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

Executable JAR file also contains the source code

I know this question asked many times but I can't find my answer from older question .I have four packages an I want to create jar file from this packages my main program is in main package . I use this statement like so :
jar -cvfm app.jar manifest.txt *
When I saw the content of a jar file my sources (.java) also exist in the jar file but I want just to have .class file in my jar file .How can I solve this problem?
You may change the command to:
jar -cvfm app.jar manifest.txt *.class
You missed the .class part. By using the wildcard * you've included everything.
Good Luck.

Running *.jar file with some dependencies to other *.jar files using cmd

I have program in Java in path: C:\...\MyProgram.
This program have some dependencies to others *.jar files. I would run it using cmd. So what I do:
in cmd I write cd C:\...\MyProgram\bin and then java -cp C:\...\MyProgram\*;. main.Main. It is working. But now I exported MyProgram to jar file. Could you tell me how can I run this now? So I have file MyProgram.jar with these same dependencies. How run it by using cmd?
Folders and archive files
When classes are stored in a directory (folder), like /java/MyClasses/utility/myapp, then the class path entry points to the directory that contains the first element of the package name. (in this case, /java/MyClasses, since the package name is utility.myapp.)
But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this:
% java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
Found in http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/classpath.html
You need to add a Class-Path entry to the manifest file (META-INF/manifest.mf) inside the jar:
Class-Path: /C:/.../MyProgram/ .
This assumes that there are dependent classes under C:/.../MyProgram/, not jar files.
You should also add an entry for the Main-class:
Main-Class: main.Main
Then just execute your jar as
java -jar MyProgram.jar
Set the path of JAR file in your classpath and then execute the other JAR file.
To add JAR using eclipse.
Right click on project -> properties
Java Build Path -> Click add external JARs.
This will add JAR to your classpath.

Is there a possible way to copy a file to a jar file?

I would like to ask a question if there is a way to copy a file(i.e. an image) to a .jar file, because i want to deploy a program and i created folders along with the source codes(they are in the same location as the source codes) and the folders are inside the jar file (i used Netbeans to create the jar file)..... Now what i want is i would like to copy files choosen by a JFileChooser to the folders inside the jar file????
Any idea is heartily accepted..!!! Thanks in advance????
P.S.:
I already tried searching the net for answers but all they know is how to copy the file inside the jar file to another directory......
Suppose that you want to add the file images/image1.gif to the JAR file.
jar uf <fileName>.jar images/image1.gif
To change the directory use -c
jar uf <fileName>.jar -C
in this command
jar uf jar-file input-file(s)
In this command:
The u option indicates that you want to update an existing JAR file.
The f option indicates that the JAR file to update is specified on the command line.
jar-file is the existing JAR file that's to be updated.
input-file(s) is a space-delimited list of one or more files that you want to add to the Jar file.
Related Docs
A JAR file is a ZIP compressed file.
See this S.O. answer for a solutiion to add files to an exisiting ZIP archive: Appending files to a zip file with Java

How to make a jar file from code?

I have some java code written in a text file, now I want to create a jar file to use that file as a library in my android project. So what would be the best way to make a jar file from this point. Please note that I have some code written in a textfile and saved as a .txt. If I rename that file to .java and use this code
jar cf filename.jar file(s)
a jar file is created but when I decompile it in java decompiler it doesnt show the packages and that codes that's why I am unable to use it's methods. What would be the best way to do this? I need help
If you want to use the classes in your library they must be compiled and you have to manually create all the packages subfolders.
So :
1. Make a folder
2. Create each package subfolderds in it (com/foo/bar/xxx)
3. Compile each of your .java files and put them it the correct subfolders
4. Zip your folder and rename it with a .jar extension
But it is hard to do this by code, why don't create it with your IDE ?
Creating a jar File in Windows Command Prompt
Start Command Prompt.
Navigate to the folder that holds your class files:
cd \mywork
Rem -- Set path to include JDK’s bin. For example:
path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Rem -- Compile your class(es):
javac *.java
Rem -- Create a manifest file:
echo Main-Class: DanceStudio >manifest.txt
Rem -- Create a jar file:
jar cvfm DanceStudio.jar manifest.txt *.class
Rem -- Test your jar file by trying to execute it
Rem -- This will not apply for a library JAR file without any main
java -jar DanceStudio.jar

Categories

Resources