How to read classes from a jar - java

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

Related

Class files (.java) inaccessable from Jar

I created a Jar file.
When I use it in my project it seems to be able to find the Package name just fine but I can't use the classes within.
Package name, but no classes after.
Did I do something wrong when creating the Jar?
I ran "jar cvf Adapter.jar *".
Any suggestions would be a great help!
A Java JAR file is supposed to contain compiled .class files, not .java source files.
You'll need to compile your files using javac and then use jar on the output.
As NimrodArgov pointed out, your IDE (apparently) can't find the class because it does not exist as compiled .class file as it would be expected. The JAR package only contains the Java source files.
As far as I can see, there is no need for you to pack your classes into a .jar file either. Just make sure the containing folder structure is in your Java Classpath.

Converting to jar or exe

I have around 10 java files with their corresponding class files. The whole application works when i execute one of the main class. I execute it using cmd. I need help converting it into a single self executing file. eg: exe or jar
A jar file is little more than just a zip of all classes and resource files. You need to add details into META-INF/MANIFEST.MF file so that the class containing the main method to be executed is know.
This should help: Link
You can try invoking the jar file as follows:
java -cp <jarfile.jar> <Complete.Package.ClassNameWithMainMethod>
You can make jar by Exporting the project using following methods in Eclipse IDE
File->Export->Java->JAR.
Note: This is already have a solution to change to exe or JAR.
How can I convert a .jar to an .exe?

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.

Use project's own .java files as resource files

I have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements
String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));
where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package
The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.
However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.
And of course when someone tries to add those source files to be used automatically as resource files the process fails.
Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.
I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:
On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.
This causes class files be compiled in the same directory as the .java files and finally does what I wanted.
Thanks to #AndrewThompson for suggesting to try this

How can I create a .jar file?

In the tutorial I found out that jar files can be created in the following way:
jar cf jar-file input-file(s)
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
But now it is not clear which .class files should I put there. After the compilation of .java files I have a lot of .class files. One of the reason of that is that I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file? On the other hand other class files corresponds to classes used by the application.
My software use external libraries (during compilation I specify .jar files of these libraries). Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
However, it was not clear what are the input-file(s). Is that .java files or .class files? From the examples on the same page I can assume that should be .class files.
Yes, you need to include the class files.
I have a lot files like that: GameWindow$2$10class, GameWindow$2$7.class and so on. Should I include all of them into the command line for the creation of the .jar file?
Yes, these are from inner classes; you need them as well.
To run my application I use java Game. So, my be I need to use only Game.class file when I create a .jar file?
No, class Game will use other classes, which in turn use others. You need them all.
Will .jar file of my application be able to run on the computer which does not contain the .jar file of used library?
No.
That said, creating a JAR manually is a good learning experience, but not something you'd really do in practice.
You should probably look into automating you JAR building. Check out e.g. Ant: http://ant.apache.org/
You can use a wild card to add the classes in the current directory into the JAR-file.
jar cf mynewjar.jar *.class
When you compile your source file into byte code, all classes inside that source will be generated as separate .class files, so unless your game.java has more than the Game class, the game class would be sufficient.
The Jar-file could contain any file you want, but to hold a Java-program you need at least the .class-files, so you have to include them. The Game-class you are talking about may be dependant on the other classes. You can check that: delete all .class-files and recompile only Game.java (javac Game.java). All other classes that are compiled are a dependency. All of these have to be included in the Jar-file, that your program can be run. The class-files generated, that have not a corresponding .java-file (i.e. your GameWindow$2$7.class) are inner anonymous classes, in your example a inner class of the class GameWindow. If other libraries are needed, these must be present on other computers, that your program can be run. But you can include the content of the other jars into your jar, so that all that is needed is bundled into one file.
The .jar file must contain all your classes that are needed during runtime. That includes the GameWindow$2$10 classes, they are the anonymous inner classes that you wrote in your GameWindow class.
Other .jar files are not included in your .jar file, but you can reference them using the Class-path attribute in your manifest.
regarding your external dependencies, this will NOT work unless you compile in the dependencies as .class files, or use something like FatJar http://fjep.sourceforge.net/ to add them into one big jar.
You can create a .jar file, zipping the folder with the compiled classes and renaming the file.
For instance, if your class files are located in the "game" folder, zip it to game.zip, and rename it to game.jar
This is really simple and it works!

Categories