I have a java jar file consists of some classes that each class has some variables, constants, functions,...
I want to extract the name of these classes, variables, constants, functions and function parameters in my Delphi program.
A work like that JD-GUI does. How can I do this? Is there any java sdk command line to do this ?
I once wrote a class-file parser for Delphi. You can find it here:
http://essmodel.cvs.sourceforge.net/viewvc/essmodel/essmodel/Integrator/CodeIO/JavaClass/
It is from 2004 but should still work unless the class-file format has changed considerably since then.
The parser is part of the freeware tool ESS-Model that creates static class diagrams.
Note that the parser works on class-files only. If you want to parse jar-files you need to first unzip the jar-file and extract the containing class-files (a jar-file is just a standard zip-file with the ".jar" extension).
Emil M. Santos has done the job for you.
He is the author of Class Explorer : an utility for browsing the low-level contents of a Java compiled class file (.class).
(source: codexterity.com)
You can grab its (Delphi for sure) source code here.
Good luck.
Related
I want use java code to run some clojure files dynamically which are in some zip files.
If the clj.p1.core.clj is on the class path, it can runs correctly.
require.invoke(Clojure.read("clj.p1.core"));
How to make it dynamically?That is, put clj.p1.core.clj in the a1.zip (maybe some files), the java program could select the zip and then run it?
Probably, you should unzip those files first and then specify a *.clj file when invoking Compile class; take a look at its sources.
What would be much better in your case is to compile a Java class from Clojure sources first and then load that class in Java as well. Just add a specific step into your build process that cares of it. In that case, your Java code will look much simpler and wont' waste time on loading Clojure code dynamically.
Creating a Java file would be easy; just wrap Clojure sources with additional namespace with gen-class declaration. Move its output into your Java project or specify classpath properly. See gen-class page for more examples.
As part of a project I am working on I am required to use a class to write serialized data to a file, but all I have been presented with is the .jar file and a rather opaque usage example. Is there any way I can glean some information on it, such as inherited class and public methods?
You can unzip the jar using jar xf {jarname} and then use a decompiler on the relevant file(s). It won't give you the source, but you will get the structure, field names etc.
Your IDE should be able to tell you all this. Just create an instance of the class and type
inst. Ctrl+Space
You should now see a list of public fields and methods. If that doesn't work, try javap to dump the API of the class (there are also options to dump the byte code) or a Java decompiler like jad
Go to your java JDK directory (i.e C:\Dev\Java\jdk1.6.0_26\bin) use command jar xf "jar_name".
Than go to Download JAD and download decompiler. Then decompile classes from unziped jar.
I have a Java File # C:\Drive\MyFile.java
Now I want to read all the methods inside the java file.
I know about Class.forName() but how to pass local path inside the forName.
you would need to compile or at least parse the java file and look for methods. you will have better luck loading the compiled class file and using http://download.oracle.com/javase/6/docs/technotes/guides/reflection/index.html
There is nothing in java that can help with .java files, just .class file. Even then, you are not assured of successfully loading an arbitrary class file, since you will probably be missing a dependency.
If I had to do this, I would run javadoc with the -public, -package, -protected and -private on every .java file I found. This gives me an HTML file with a regular format that can easily be parsed. If I was more ambitious, I would write a custom javadoc doclet. If I was crazy ambitious, I would use antlr, yacc, javacc or another parser generator with a Java grammar to parse the Java files directly.
Try using reflection, which allows you to find variables, methods, constructors, etc. defined in a Java class.
I am a .Net developer starting Java development for Android and would like to know if it's correct to think of Java packages like .Net assemblies.
No.
The best comparison would be with a Java ARchive (Jar) file. Java uses packages to control the namespace, and is very similar to C#'s Namespaces.
Here is how I'd compare the environments
Java .Net
==== ====
Class Class
Package Namespace
Jar Assembly
No, I think a Java package would be more similar to a namespace
And an assembly would be more like a jar (I'm not so sure about this, as I'm much more familiar with Java than .Net... correct me if I'm wrong)
A Java package is like a namespace in .NET.
The equivalent to an assembly in Java is jar file.
A package in Java usually means just a namespaces for classes and interfaces, which in reality means a specific directory structure. Sometimes a .jar file is also referred to as a package. This is probably the closest you get to an assembly. A .jar file can also contain other data like images, or basically an kind of file since it is just a zip again with some specific content structure.
In any case: Usually when you read "package" in relation to "Java", some kind of namespaces (via folder structure, e.g.: com.mycompany.myproject) is meant. It doesn't help that some build tools refer to the process of building .jar file as "packaging" ;-)
Bear in mind that I'm far from a Java expert, but anyway, there it goes my take on this:
As pointed out by other answers, a package is not the equivalent to an Assembly. However, I don't fully agree with the idea of a .jar being the equivalent to an Assembly.
To me, a Java class (contained in a .class file) is closer to an Assembly than a .jar is. I'm saying this cause while the load unit for the CLR is the Assembly (which means all classes contained in that Assembly get loaded), the load unit for the JVM is a class (when the JVM needs a class, the ClassLoader does not load all the classes in the container jar, it just loads that specific needed class).
You can read more about java class loading here:
When does the JVM load classes?
and here:
http://www.lansa.com/support/notes/p0294.htm
Is it possible to convert a .class file to .java file?
How can this be done?
What about the correctness of the code extracted from this option?
It is possible. You need a Java Decompiler to do this.
You'll find mostly it'll do a surprisingly good job. What you'll get is a valid .java file which will compile to the .class file but this .java file won't necessarily be the same as the original source code. Things like looping constructs might come out differently, and anything that's compile time only such as generics and annotations won't be re-created.
You might have a problem if the code has been obfuscated. This is a process which alters the class files to make them hard to decompile. For example, class and variable names are changed to all be similar so you'll end up with code like aa.a(ab) instead of employee.setName(name) and it's very hard to work out what's going on.
I remember using JAD to do this but I don't think this is actively maintained so it may not work with never versions of Java. A Google search for java decompiler will give you plenty of options.
This is possible using one of the available Java decompilers. Since you are working from byte-code which may have been optimised by the compiler (inlining static variables, restructing control flow etc) what you get out may not be exactly the same as the code that was originally compiled but it will be functionally equivalent.
Adding to the previous answers: recently, a new wave of decompilers has been coming, namely Procyon, CFR, JD, Fernflower
Here's a list of modern decompilers as of March, 2015:
Procyon
CFR
JD
Fernflower
You may test above mention decompilers online, no installation required and make your own educated choice.
Java decompilers in the cloud: http://www.javadecompilers.com/
It is always possible. Search for "java disassembler".
But source code comments and temporary variables will not be available.
If you decompile a class and see the code is too complex with variable names and method names are like a,b,c... that means that the project is obfuscated.
Not exactly a decompiler, but the JDK contains javap, a disassembler:
javap -c org.example.MyClass
Depending on your usecase, it might still be interesting to know or use.
Note that results of class file decompilation depend on the included information within a class file. If I remember correctly, included debug information (see -g flag of javac) is important, especially for naming of variables and the like.
DJ is the easy to use java decompiler . Just open any .class file and it will show you its java code.
Also, you can use jadClipse plugin for eclipse to directly decompile the .class into .java
What about the correctness of the code extracted from this option?
In any case, the code which will be generated by any java decompiler will not be the same as it was written in orginal java class. As it just decodes the bytecode into java code. The only thing you can be sure is, that the output will be same as the output of orginal java code.