How can I decompile many jars at once? - java

Well, I guess subject says it all :)
The ideal solution would find all jars within a certain folder (they might be in sub-folders), and write all the sources found into a single "src" directory, of course maintaing the package folders.
Concrete use case: decompile all Eclipse plugin jars

Download JAD Decompiler.
Unjar all your jar files (using the command jar xvf) to some directory. Let's call this ${unjar.dir}.
Create a directory for JAD to write out the decompiled sources. Let's call this ${target.dir}.
Execute the following command:
jad -o -r -sjava -d${target.dir} ${unjar.dir}/**/*.class
options are:
-o - overwrite output files without confirmation
-r - restore package directory structure
-s <ext> - output file extension (default: .jad)

http://java.decompiler.free.fr/

Decompile eclipse plugins??
Apart from the existence of a technical solution - (1) you can get all source files for the (official) eclipse plugins as bundles and (2) don't expect that the decompiler results will compile or can be used to debug the code.
So if you want to study the source of eclipse and it's plugins, decompiling is .. say .. not the best idea.

I wrote a tool names code-collection using shell script and cfr to find and decompile all jar, war files at once.
After decompiling jar, war files, you could use my tool to find and copy all specific files (.js, .html for example) to a new directory.
Visit my project at: code-collection

The Windows equivalent command for this would be:
jad -o -r -sjava -d "target.dir" "unjar.dir/**/*.class"

Related

Can javac process/include `.properties` files so that they appear in the output?

Currently I'm building a small personal project in Java which is a simple file server. I've implemented basic internationalization with usage of ResourceBundle. I'm using .properties files to store messages in different languages.
Till now I was using Vscode built-in java compilation process which was copying .properties files into its corresponding directory in the output. However now I'd like to write a build script for that project.
My command looks like this:
javac -d ./bin -cp ./src ./src/**/*.java.
This command however doesn't take .properties files into account. I searched web whether javac has ability to somehow process/include this files into output but found no answer. I know I can use Maven or Ant, but I'd like to make this project without usage of additional tools.
Answering to my own question, but maybe someone will find this useful one day. In short javac always compiles .java files producing .class files. There is no way that it could process some other file (source:
How to set the output files when compiling with javac).
Long story short if you want to include resources, images, text files, anything that is not .java file in your output, and don't want to use build tools, you have to copy it manually or use cp command in your build script.

Compile java from multiple directories [duplicate]

This question already has answers here:
Java: How can I compile an entire directory structure of code ?
(10 answers)
Closed 7 years ago.
I'm trying to compile multiple .java programs from different directories either on Windows, Mac, or Linux... e.g. cmd or terminal. It doesn't matter.
However, I'm sure that many of you are familiar with how Netbeans stores files in different folders. I have been putting different concepts into different folders, and now I want to run all of them.
For example, a chess program that I have looks sort of like this:
/Chess
/build
/classes
/chess
/Chess.class
/color
/colorHelper.class
/game
/Board.class
/Game.class
/GameManager.class
/Player.class
... etc. (the rest of the directories with .class files)
/build.xml
/gameLog.txt
/manifest.mf
/nbproject
... (some xml and .properties files)
/src
/chess
/Chess.java
/color
/ColorHelper.java
/game
/Board.java
/Game.java
/GameManager.java
/Player.java
... etc. (the rest of the directories with .java files)
So, my question is, how can you use javac *.java (in /src probably) to compile all of the files (because otherwise I get a cannot find symbol error. Since I get a file not found when I run javac *.java in src, I am at a loss.
Thanks in advance,
Dylan
It won't compile in one go.Actually,your src directory doesn't contain any .java file,SO it won't be done in that way!
I am afraid that you'll have to do it by changing your path under src folder to do the same.
You'll have to perform for each chess,color,game,etc. directories to achieve the same.
So,change path at each run or go as advised in the comments to achieve compilation of all the java files.
OR
As proposed by David Ehrmann in your comment,you can do it by compiling in one go using javac $(find . -name '*.java').
It will compile all .java files under your present directory(src(.)).
javac *.java compiles all the Java files within the current directory. In your case, since you have different hierarchy of folders based on the package, you cannot achieve this in one go. At best you can run the command javac *.java on each folder.
You should try using an automated build system like maven or ant. It will allow you to build your project with a single command. It will even allow running unit tests, and packaging your application as a jar (or whatever package you prefer).
If you really don't want to use one of those you can still do it with a single (albeit lengthy) command. The *.java is just a wildcard in a file path. Its looking for any files in the current directory ending with .java. On linux you can try something like this find -name "*.java" | xargs javac. The first part is a find command that searches recursively for all files ending with .java. It this pipe's that list of files to xargs which adds them all as individual arguments to the java command.
That really isn't the best way. Again, try maven. Its pretty simple to set up for a basic project, your IDE usually has a feature to create a maven project for you as well.
to compile more than one file having different package name in same folder use
javac -d . *.java

java.lang.NoClassDefFoundError. Class not found during runtime.

I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.

prog.java and prog.class in Prog.jar file - How can I easily update this code?

Back story:
Long ago, before dinosaurs were around, there was a contractor that wrote a small Java program, that does some stuff, and prints some stuff to the screen.
Current: No one here knows much about Java but me - and I am not fluent.
The program works, but we kind of need just a bit more information on the screen. From the documentation available, I think I know how to go about that, and I definitely know how to print stuff, as it's simply using System.out.println() for this.
I have a .jar file, which I'm 99% certain is being loaded/used, which contains .java files matching every .class file within.
Is there an easy way to (and how might I) slightly modify one of the .java files, and "compile" the new version to replace the current matching .class file within the .jar?
I'm able to open and read the java source file, of course, but don't remember to procedures to turn java into "class" files, and especially not sure if I can just drop the resulting .class file into the .jar file as a replacement.
at the basic level, javac is the compiler; that will produce the .class files. There is also a "jar" command that will regenerate your jar file. Depending on the number of dependencies, that can get complicated quick. You can just type "javac" and "jar" (w/o quotes) to get the args to run it. Another option is to setup an ant build script...
so you could do something like (assuming windows OS):
javac -cp %MY_CLS_PTH% *.java
where MY_CLS_PTH is where any dependent classes are. If you have a package structure, this gets a little more complicated.
jar -cvf .\my_class_dir MyJarFile.jar
where my_class_dir is the directory that contains the .class files.
I think that is right (didn't run it myself) but that should be about the basics...
EDIT: There is a way to just add/re-add a single .class file to an existing jar file. Check out the "jar" command's usage, looks like "-u" will update it (of course you have to pass in the .class file). Also remember that any package structure you have in these classes, you need that directory structure and you reference the class via that dir structure. (Hope that is clear...)
Add myclass.class to the top level directory in myjar.jar:
jar uf myjar.jar myclass.class
Add myclass.class to the subdir directory in myjar.jar:
jar uf myjar.jar -C subdir myclass.class
Add a Version attribute to the manifest of myjar.jar:
First create a file somefile containing the line Version: "X.Y.Z", then
jar umf somefile myjar.jar

How to run Javah from Eclipse

So I'm trying to run the 'javah' tool on a compiled .class file in Eclipse, but I have no idea how to do it. The examples I found simply said something along the lines of 'run javah on your class...' but I don't really know where I'd find such a command line in Eclipse.
If someone can give me a set of idiot proof instructions get this done in Eclipse, I'd be grateful.
Thanks :)
AFAIK Eclipse does not integrate javah by default. You have to set it up as external tool yourself.
Create a new external tool
Set the path to javah (on linux this
was /user/bin/javah)
Set the working dir to ${project_loc}/bin/ where bin is your Projects output directory
Add ${java_type_name} to the arguments
With this setup you can call javah as external tool on any java file in the Package explorer.
The generated header files currently land in the bin dir, this can be changed by adding the -d option.
Here is a sample command line:
javah -classpath /path/to/project/classes com.mycompany.MyClass
/path/to/project/classes - This is the 'Output folder' from the Source tab of Java Build Path properties page for you project.
It may be relative to the directory from where you are running javah.
You may use -verbose flag to see more details about what's going on.

Categories