I have a jar file which includes seviral classes. In that jar there is a Confirmation.class file which i want to edit. I decompiled that class by usin JAD. Then i edit it with notepad++ and saved as .java file.
Now how can i create my new jar file with other classes?
Other files format is .class but mine is .java, is it problem?
If it is , how can i compile my .java class ? (when i use command javac Confirmation.java it gives errors and want other classes)
Thanks..
Do you have dependencies to other jars?
If this is the case, you will have to put them on the compiler classpath in advance.
In any case, just decompile all the package (I use jd-gui), change the class, compile, open the original jar as a zip, put the new class on it, and you are done.
Another approach is to create dummy classes for the missing dependencies...
Personally, I really discourage this approach of "reverse engineering" working with an already compiled package, but I understand situations where you don't have access to the original source code, but you need to fix something urgently (ex: working in a company, where your code comes from an external provider on the other side of the planet...)
You may try to copy & paste the code in Java IDE such as Eclipse, NetBeans, etc and ask the IRC to compile for u. Have a nice day :)
Related
Sorry if the title doesn't say what I'm trying to do very accurately but I wasn't really sure how to describe it in one sentence. Basically I have a jar file with a java file inside it, and I want to edit the java file. I used 7zip to do so and it worked, letting me edit the code in the java file. I saved it and my new code shows up when I open the java file in netbeans. However, the new code isn't actually in effect, and my program is still using the old code. I don't know if this matters, but the java file is named Templates.java, located in a package called net.sf.dynamicreports.examples in a jar named dynamicreports-examples-5.0.0-sources.jar.
Thanks in advance for any help anyone can offer.
Edit: tried importing the jar into a new project and editing from there before putting it back into the original project, but had no luck (check comment chain for further details). Still have no idea how to fix this and would greatly appreciate any help
Your jar contains .class files and .java files.
When you create the jar, the .java files are compiled and .class files are created from them. You should have a Templates.class file that corresponds to Templates.java.
When you edited Templates.java, Templates.class was never recompiled. This class file is the one your program uses.
The appropriate way to change a jar file is unfortunately to regenerate the entire jar.
So, what you should do is open the code you used to create the jar in the first place, make your java changes there, and create a new jar.
You will need to do this even with the smallest changes.
I didn't make the jar myself unfortunately, I got it from somewhere else so I don't have the code used to make it, just the jar itself. What should I do?
Edit: I saw your comment that you did not make the jar in the first place. This isn't too big a problem since you do have the java source code. What you need to do is copy all the .java files in the jar and make a new jar with them. Or, simply use Eclipse or whatever your favorite java editor is to compile your Templates class, and copy the new Templates.class file it creates into the appropriate place in the old jar, overwriting the old one.
Where would I find the Templates.class file?
Edit 2: You find the class file in the bin folder of your project if you are using Eclipse. See this question: Find the .class file compiled by Eclipse or if you are using a different editor/compiler, search google for where it stores its .class files.
Can someone please enlighten me how to organize files in my Project?
I work with the command line. I don't want to bother with IDE Tools for now. My tiny program consists of just one .java file, but uses a bunch of 20 libs or so.
This is what I did. In the first line of my .java I wrote package com.mycompany.app.module; The .java and later the .class file are located in
/home/sweethome/Projects/Proj1/com/mycompany/app/module
Next I wanted to create a jar to put package that would include
com/mycompany/app/module/LogonToken.class
Is this ok so far? Here comes my real question: where do I put the 20 libs this class needs to run?
com/mycompany/app/module/libs
The clean way to handle external dependencies is to use a build system like Maven, and declare them in a pom.xml file. This does not require an IDE.
At the very first moment, this seems to require some extra effort, but will actually amortise very fast.
Does your question is build jar file with 20 libs and class? If it's true, here is answer of your question
Creating a JAR File
Here is the answer to my own question: http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/
Fairly standard java project consists of three top level folders:
/bin - empty folder that will contain compiled .class files
/lib - contains third party .jar files
/src - contains .java source files
There doesn't seem to be any code there. I expected to see class declarations so I could see what the code does but instead there's some
Are they somehow precompiled? What's the difference between included Jar file and a pure code?
You are looking at .class file, which is a generated when you compile .java file. To see what the program does, you have to look into .java file. You can refer official java documentation for that.
A .jar file is packaged file with .jar extension, it contains compiled java files and their class files. This file is usually imported into projects to use the classes defined in that package.
You can use "jar xf jar-file" command in command-prompt/terminal to extract the files from jar and look into the package.
A JAR will normally contain compiled class files. It may also contain source files or there may be a separate JAR that contains the source files, but not necessarily so.
If you want to use the library in your project, then a JAR of compiled class files is what you want. If you want the source code, then you'll have to see if it is available from wherever you downloaded this from. If all you want is to see how to use the classes, then probably what you want are JavaDocs for the library you are using. This is an HTML based API documentation.
Well, this is because you haven't attached any source for the mentioned dnsns.jar. You can attach source to existing JAR files in Eclipse. Refer this SO post: Is there an easy way to attach source in Eclipse?
For this specific dnsns.jar, it is part of your JRE, and if you are not able to see its source in your IDE, then it means that the Java that you have setup in IDE lacks the source. If your installation does not have the source (src.zip), then you can get it manually as mentioned on this SO post: Where to find Java JDK Source Code?
EDIT: Alternatively, you can also use a decompiler (e.g. http://jd.benow.ca/) to reverse engineer the source from byte code, though, it may not be the exact match to the original source but you can understand the overall idea. You can add the decompiler as the default program for opening .class files in eclipse Windows > Preferences > General > Editors > File Associations. Select *.class filter and add your decompiler as the program. Though, it is not as clean as attaching the source to JAR, but may work if you don't have access to source.
EDIT2: About your question
What's the difference between included Jar file and a pure code
Eclipse can find .java files for your own code because obviously they are in your workspace. But when you add a JAR file as library, it may have the source (.java) in it or not. If the source is available, eclipse can display it by default. If not, you have to add it manually.
I need to edit one line of code in the EJB. I import the ear into eclipse, but I cannot figure out how to edit the .class files. I can view them using Jadclipse, but I cannot edit them. I edited the .class file manually and put the revised .java file in the EJB and tried to export the ear, but it just deleted my .java. I guess my question is really how do I recompile my .java file into my project. I need to import an EAR, edit a .class file and then export it as an EAR. This project was written for the business I am contracted to over 4 years ago. The developer that wrote it is no longer with us. So all I have is the EAR. Any help is appreciated.
Get yourself a good hex editor.
You can try decompiling the class files back into Java using a decompilier tool such as JD:
http://java.decompiler.free.fr/
Decompiliers are not perfect, however. The code they generate may not always be correct, so use with caution.
You don't edit .class files. It's the .java files that you edit and then compile into the .class files. I am assuming that the .java files must be in there as well if you can view them and update. Of course it is pointless to try to put the .java back in and export since the runtime will require the .class.
Of course, if it is EJB code inside an EAR, it is quite possible that it is either generated code, or code that will have to be run through an EJB compiler to produce a proper EJB.
You cannot edit class file, but you can try to edit bytecode in runtime, try javassist
An EAR file is just a zip file. Rename it to .zip and you can view it in WinZip or whatever.
Then if you are lucky you can decompile the file and make any mods you need to the file (I say lucky because it depends how the file was compiled, obfuscated or without any debug info).
Use ANT to rebuild the EAR (See How do I create an EAR file with an ant build including certain files? or some other resource)
You should be good to go, just redeploy the new EAR.
I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.
You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.
You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.
As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar
If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.