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.
Related
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.
I'm trying to cmd line compile a .java selenium test script into a class file that I can run from the command line.
All of my selenium jar files and all other supporting jar and lib files are in C:\JarFiles
My CLASSPATH is set to C:\WDJarFiles*
I am working at the command line here: C:\EclipseIDEworkspace\MC3\src\Tasks
My .class files are located here C:\EclipseIDEworkspace\MC3\bin\Tasks and I'd like to be able to update them at that location.
My folder structure was set up by using Eclipse IDE so I'd like to keep the existing folder structure but now I want to be able to compile my .java files from the command line and update the .class files.
So, when I run javac like this:
javac Edit.java
It compiles OK and the .class file gets created in the same folder where I am running the javac command -- but -- I also get a huge number of other .class files in this same directory! These look like supporting class files.
I'm not sure what my cmd line javac syntax should be to:
Compile my .java file so its .class file gets updated in the C:\EclipseIDEworkspace\MC3\bin\Tasks folder.
I don't get all those other .class files created in my working folder C:\EclipseIDEworkspace\MC3\src\Tasks
Thanks for any help...
You should try the '-d' option to specify an output directory:
javac Edit.java -d ..\..\bin\Tasks
About the multiple other .class files, you probably have many nested classes into your Edit.java file?
There is a jar file already deployed and successfully running. And now we need to add a very small string in a class.
These are the steps so far i made:
1.) I already decompile/extract the jar into a folder.
2.) Open a class file in JDgui(java file viewer).
3.) Copy the source code in notepad++ and save as fileName.java
4.) Compile that .java as .class via cmd.
5.) Compile the folder again as .jar file (the same as the old .jar which is running)
Now when i restarted Tomcat, run the system, then it showing the error:
ClassNotFoundException
When i open that jar file in JDGui the class is in it.
Please help. Thanks
This is no doubt coming from the error you naming the class differently or placing it in a different package (different folder).
When you save source of the class in a Java file, make sure you save it in a folder that reflects its package, and also place the package statement at the beginning of the file, reflecting the folder structure properly.
Also note that if the class is declared as public, it must be in a file named after the class: ClassName.java else it won't even compile. So you saving it in filename.java will not work in this case.
Also make sure you get no comile errors when you recompile this java file after modifying it (else you will not get a result .class file).
I am trying to package up some java code which has references to the Hadoop API for java. I am writing the code on my PC and moving it to a CentOS VM and then compiling it.
For some reason when I run the javac command and then un-jar the product file my ./META-INF/MANIFEST.MF file does not have a line which defines the main class such as "Main-Class: folder1.folder2.file". I believe this is why I am getting a "no main manifest attribute when trying to run Hadoop.jar".
Just to step through the process I have the code written using netbeans and import the Hadoop API .jar file. After saving I upload Hadoop.java (this is the only file with code other than the Hadoop API) file to the VM in the /usr/dan directory.
From /usr/dan I run javac -classpath /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar -d ./Hadoop ./Hadoop.java there aren't any errors displayed.
I then use jar cvf ./Hadoop.jar -c ./Hadoop/ to create the jar file. There aren't any errors displayed.
I then try to run the jar file with java -jar Hadoop.jar and get the error.
Is the only important file I need to upload to the VM the .java file? I have the Hadoop API jar file in /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar?
Once I execute the javac command the API is included in the .class file and I no longer need /usr/hadoop-0.20.2/hadoop-0.20.2-core.jar, correct? Or does this file need to be retained in a certain path relative to the .jar file?
What are the possible reasons the manifest file would not have a Main method defined in it?
Much appreciation for any help, I've been racking my brain on this all weekend.
EDIT:
By default the manifest include nearly empty, as stated here:http://docs.oracle.com/javase/tutorial/deployment/jar/defman.html. I can easily add an entry to the manifest file with the m flag. I based my usage off of http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html. It seemed to me that this was something which had to be done after the jar file was created, but just adding the m flag with the appropriate components when initially jarring the file will do the trick in one shot.
If you create your jar through the jar command and want additional manifest entries (such as Main-Class), you'd need to use the m option
jar cmf manifest-file jar-file content-files
with a suitable manifest file (which can contain plain manifest entry lines).
In hadoop, it is not necessary that you need to have a Runnable Jar to execute. Hadoop come up with a utility for running Jars. You may use the following command for executing a Jar, which is not Runnable (Main class is missing in ./META-INF/MANIFEST.MF)
hadoop jar Hadoop.jar <PackageName>.<MainClassName> /input /output
If you don't have any packages use the following command
hadoop jar Hadoop.jar <MainClassName> /input /output
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