How to recompile jar file from command line - java

I have a jar file that consists of class files and java source files together (actually android unity plugin).
I want change the behaviour of one of the function by modifying the java source code and repackage it to jar file. Is it feasible to do with command line?

Use jar xf <JAR-file> to extract the entire JAR file to whatever directory you're currently on.
Add your new code to the files, removing the old code (make sure you have copies or back everything up, just in case).
Use jar cvf <JAR-file-name> * to create a JAR using all contents in the directory of your files.

Related

Running GUI Application without IDE

Lets say that I built a GUI Application using NetBeans. To run this java application I need to open source code in IDE and then run. I know that I can also run through command prompt.
But how do I start the application independent of IDE. Isn't there some .exe file or something like that, which on double clicking directly runs the application?
If not, how do I generate such a file?
Here you can find how to create .jar in Netbeans: How to create a Jar file in Netbeans
You can run the executable jar on every single computer, on one condition - the system have JRE installed.
If you want to, you can also build the .jar using command line, to do that use the following command:
jar cf jar-file input-file(s)
Description from Oracle doc:
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file. The f
option indicates that you want the output to go to a file rather than
to stdout. jar-file is the name that you want the resulting JAR file
to have. You can use any filename for a JAR file. By convention, JAR
filenames are given a .jar extension, though this is not required. The
input-file(s) argument is a space-separated list of one or more files
that you want to include in your JAR file. The input-file(s) argument
can contain the wildcard * symbol. If any of the "input-files" are
directories, the contents of those directories are added to the JAR
archive recursively. The c and f options can appear in either order,
but there must not be any space between them.
This command will generate a compressed JAR file and place it in the
current directory. The command will also generate a default manifest
file for the JAR archive.
After you build your application look for a folder named "dist" in your project's folder. You should find there a file *.jar which can be run anywhere with double click.
STEPS TO FOLLOW:
create a jar
run the jar

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

Tool for creating jar files from command line

I'm not very skilled in writing batch files and/ or java. I have a folder with several .class-Files and folders in it and I want to put them all into a executable .jar-File. I know that there is a tool called "jar - The Java Archive Tool", but that just won't work for me. My folder structure looks like this:
Folder "test"
File (a.class)
Folder "subdirectory"
File (b.class)
I want a executable .jar-File called file.jar. In this .jar should now be the file a.class and the folder subdirectory with the file b.class in it.
I don't get the .jar-Tool to run and the 7zip command line doesn't support .jars (I can't even add files to it). I want this to run from a .bat-File, so I just have to open the batch-file, it creates the .jar and puts the files in it and then closes itself again.
I hope you can help me and get what I mean.
If you have issues in executing jar command, then probably you would need to check if your path has been set correctly. You can verify if the path contains jdk location ( echo %path%) from command prompt. If not you need to update. Also you can verify using Javac -verbose command to see jdk is installed.
Once you have jdk path set, you can try below command to create jar
Jar -cf myapp.jar * --> includes all files including files from sub folders.
If you want to run from batch, you would need to mention path before jar command. Ideal place for path is to configure as environment property.
Create a text file (and call it something useful like manifest.txt). In it, put the line:
Main-Class: a
(which should be called A by convention) and include a hard return at the end.
Then create the jar with:
jar cvfm file.jar manifest.txt *.class
or
jar cvfm c:\somedir\file.jar c:\somedir\mainfest.txt *
You could just put that line in a batch file called createJar.bat and run it if you only want to create files called 'file.jar'
hth

changing code in Java class file with dependencies

I started off with a .jar file that I unzipped. I need to change a few lines of code in just ONE of the classes contained in the contents of that jar file. I went about this as follows:
1) opened the class in Java Decompiler to view the source.
2) copied source to a new text file and saved with ".java" extension.
3) in command line I went to jdk folder and executed javac Classname.java to recompile.
However this class code imports some dependencies so the recompile failed. I have the dependencies, they were part of the original jar file contents but they are all compiled .class files and spread across several folders...
Is it possible to re-compile this class successfully? Is there command line code to include dependencies?
Yes, use the -classpath option.
javac -classpath original.jar Modified.java
Then, you can remove the old class from the jar file and insert the new one. There isn't a simple way to do this via command line, so I recommend an archive application such as WinRAR or 7-zip.

including an exe file to jar

I have written a java program that is actually works as a gui to an existing command line program.
so in basic all my program does is Runtime.getRuntime().exec("myprogram parameter");. So I have exported my java source as a executable-jar file by using Eclipse IDE and it is working nice, however I indeed need to include this myprogram.exe to the directory of the generated jar file inorder to work.
Now I am looking for a way to include myprogram.exe inside the jar file so I can keep it bundled as a single file, a method using using Eclipse would be preferred.
You can simply jar it up as an extra resource (like a .css, .properties etc.).
When your Java program runs, extract the .exe using Class.getResourceAsStream() and write it to a temporary directory in order to run it from there (since you can't run it directly from the .jar file).

Categories

Resources