how to extract and update class file from a jar? - java

this is what i need to do:
1) extract files from a .jar archive
2) take two of them, decompile them, make some updates and then re-compile them
3) make the jar archive again
Basically the problem is that after my updates i cannot compile the class anymore because i ve got a lot of errors about dependencies by the javac compilator.
i'm extracting files from .jar using archive manager of linux
i'm decompiling classes using java decompiler online
i'm trying to compile the file again using the command javac
thanks please help me

Related

How to read classes from a jar

I am trying to read classes from a jar.
So I started by extracting the files under my jar, I am using JAD decompiler.
The decompiler works, for example I have written
System.out.println()
And I have tried to open the class System and it shows the implementation of the class.
When trying to read the .class under my jar, I have tried to put the .class under a package, but I can not see the .class under the package, so I have tried to open it like this : File>Open File.., I get the error :
The Class File Viewer cannot handle the given input ('org.eclipse.ui.ide.FileStoreEditorInput').
How can I see my .class?
You could open any .class files with Intellij IDE

How compile a java file to .class who use others .class. (if it's possible)

I get lib webrtc in a .aar, and I need to modify one file of this lib.
We can call this file tomtom.class for this example
So I unzip this .aar to get many .jar and in those .jar there the file tomtom.class who need to be replaced.
I have the new version of file tomtom in java because I edit it. But now I need to compile it for replacing the old tomtom.class. Of course, this file uses other classes inside this lib webrtc.
So is it possible to compile a java file who use other compiled file?
After many tried of use classpath. Or other method I find on Internet.
None work, maybe it's me who is a moron but whatever..
How I replaced my file on a lib already compiled (it's the webrtc lib).
I put a copy of the file decompiled in my project on android studio. I can modify it like I want. And this decompiled file can compile with the lib I had in the folder lib.
I build the project and I go to the temp file to get the compiled file.
Then I can replace the one in .aar by the file compiled I just modify.

Compile and run Groovy on Linux server

I'm trying to move my Groovy project to a server with no Eclipse. In order to minimize the uncertainty I compiled my project in Eclipse (on my PC) and copied the project(with all .class compiled in /bin) to the server in a whole. After that I kept meeting the error:
Error: Could not find or load main class edu.xxx.textrank.TrendAnalysis
This is how I ran my code:
All .groovy and .java are in folder .../TextRank/src/edu/xxx/textrank/, all compiled .class files are in folder .../TextRank/bin/edu/xxx/textrank/, and all dependent .jar files are in folder .../TextRank/lib/. In directory .../TextRank/bin I ran java -cp ../lib/*.jar:. edu/xxx/textrank/KeywordExtractor and met the error. TrendAnalysis.class was compiled from TrendAnalysis.groovy and there are also some auxillary .class compiled from closures in groovy:
TrendAnalysis$_allPeriodKeywordStudy_closure1.class TrendAnalysis$_certainPeriodKeywordStudy_closure3.class
TrendAnalysis$_allPeriodKeywordStudy_closure2.class
However, things are not quite same for those .class files compiled from .java. For example I was able to run Patterns.class which was compiled from Patterns.java without inner classes by the same command
[xuch#xxxxx bin]$ java -cp ../lib/*.jar:. edu/xxx/textrank/Patterns
abcdefghijk
I'm wondering what could be the real culprit. Can I just run the .class files without re-compiling them on another server? Or am I able to just run the main .class without calling all the innerclass files (auto-generated by Groovy)?
Any suggestion and help is appreciated! Also quick solutions will help (like how to compile all things into an executable .jar etc). Thanks a lot!!
Edit: One important thing to mention, all my .java and .groovy are in package edu.xxx.textrank. In other words they all have package edu.xxx.textrank;

Java Edit Jar File

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 :)

classes of jar file cannot be imported in netbeans

I created a jar file from a bunch of java files. Folder structure was org/ax/redis. I used the command jar cvf jedis.jar org/*. Then I imported this jar file in my netbeans project. Then when I tried importing classes from it, by writing import org.ax.redis.*. However, netbeans shows error that no such package exists.
Now I opened another jar file of log4j to see how it is from inside. Only difference was in manifest file. It had a bunch of directives like Name: org/apache/log4j/. So I created a manifest file for my jar file by including Name: org/ax/redis/. Used this command to add manifest information in my jar jar cvfm jedis.jar META-INF/manifest.txt org/*. Still nothing works. Please help me
Jar files typically contain class files (the results of compilation) rather than source files (*.java). While some jar files may contain both, only the class files are available to compile or run against (e.g. using -cp library.jar).
So basically, before you build your jar file, you need to compile your code - and then include the class files in the jar file. If you include the source files as well (in the same directory structure) then some IDEs may be able to detect that, which can be useful.
Even If u don't generate .class file , the package still can be imported and it won't show any compilation error(If U don't specify any particular class) . However when U try to use a particular class function of that package , it will throw an error .

Categories