Finding information about a Java class without documentation - java

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.

Related

How to use clojure files dynamically?

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.

Why don't I see code when viewing included Jar libraries in Eclipse?

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.

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

How can I extract symbols defined in a jar file using Delphi?

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.

How to dump contents of a Java library to the console?

Is there a tool for dumping the contents of a library or JAR file to the console?
I'm looking for the Java counterpart of DUMPBIN /SYMBOLS, which works on Windows native-mode libaries. With C# and the rest of .NET, there are a bunch of visual tools like .NET Reflector and the object browsers built into Visual Studio. All-in-all, I prefer command-line tools, but I will be grateful anything that works :)
(I'm just getting started with Java and Scala on Windows.)
The command you're looking for is:
jar tf <jarfilename>
You can unpack a JAR file (jar xf my.jar) to see the contents; if you want to do this within your code, there are tools in java.util.jar for working with JAR fileS.
Alternatively, if you know part of the package hierarchy, you can type part of it in the Scala REPL and hit tab to get a list of valid completions. For example:
scala> java.util.jar. <-- hit tab here
Attributes JarEntry JarException
JarFile JarInputStream JarOutputStream
JarVerifier JavaUtilJarAccessImpl Manifest
Pack200
However, it's awfully hard to figure out how to use things without API documentation. Your first step should almost always be to try to find the documentation and look at them instead.
If you know the class name:
$ javap -classpath jarfile some.package.in.that.jarfile.ClassName
Also you can "script-ize" it so that it iterates through all the classes in the jar file and run javap for each one
Also note that a JAR file is merely just a zipped file so any ZIP utility would do. And most IDEs offer to explore inside the JAR libraries you add to a project (Eclipse for instance).
As mentioned, JAR files are ZIP files and can be extracted in the same way.
To browse the classes inside a JAR file, I recommend you to use an IDE. Add the JAR file as a library dependency to your project, and then you should be able to browse through the classes and methods.
How about obtaining the source code for the JAR file (if that's possible)?

Categories