.class into .java? - java

Well, I had just finished coding a java project and went into testing it using eclipse. It was exporting fine and I went ahead and created a new file in src named "config.yml" and it all of a sudden removed all the .java files in my project!
Fortunately, I still have the .jar which contains all the .class files, but I seem to be unable to edit the code (i'm guessing this is to do with it being a .class instead of .java) so, is there any way I can transfer these .classes back to .java ?
btw, if you're recommending any software please note that i'm on a macbook so i'll be limited to what i can use..

Try this (JD-GUI). Have a Mac version and it's nice.
It will show to you the code from the class file.

Related

How can I retrieve my Java source code from a JAR file?

I was trying to create a .jar from a .java file for a school project but now I can't read my .java file. The code I had written was overwritten somehow and I need to know how to (if possible) revert my .jar file back to the code I had written. Thanks in advance because this is a time sensitive matter.
You could try "decompiling" your JAR file using any JAR decompiler, such as http://www.javadecompilers.com/.
You won't get exactly the source code you originally wrote, but the decompiler will do it's best to map the .class files embedded in the JAR back to Java source code.
If you are using Eclipse you can right click on your project and check in history for getting old code

Classpath of a decompiled Jar file

So I'm not CS major or anything and I've been just poking around stuff to practice some Java skills I watched from a video. I know this isn't the major way to do it but please, let me.
So I extracted a .jar file of this: http://www.zenlunatics.com/quizcards/ (i also have its outright source version). It's open source. I used JD gui to get the source code and got everything in .java. However, once I imported it and tried to run it in Eclipse, it says there is "no main type."
I've searched around and it seems there has to be a public static ... String[] args and I don't see that in any of the generated .java files.
I tried searching about it on youtube and he's getting .class instead of .java files. So, say I renamed .jar to .zip and extracted class files, do I just edit the main .class and build? Any tips, please.
Thanks!
Looking in the source code download the main method is in the QuizCards class (QuizCards.java).
Since there is a source code download you should use that.

converting .class into .java

I deleted a Java project from my hard disk in an attempt to do some refactoring with Eclipse. Luckily I found a recent version of an Executable Jar File and decrompressed it into a bunch of .class files.
I've read some 'decompiling' threads on SO and tried showmycode.com, but I was hoping for more. Isn't it possible to convert .class files into the .java files that made them, comments included - nothing changed? Or find the in the .jar file? What are my best options if not? Other answers on the topic seem outdated. Do I need to download software?
You will not be able to get your comments back, they are lost when you go from .java to .class. As to how to do it any "Java Decompiler" can do it, your code will not be exactly what you wrote, however the code you get back will be functionally the same as what you originally wrote.
JD is a decompiler I have used before and have been happy with its output.

Running .jar File Java 8

I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.

Java, running the code

I have a .java file that I created in eclipse. I managed to make a .jar out of it, but I don't know how to put my images folder into the jar, nor do I know how to make the jar run?
Should it run already, or would it just be an archive like a .zip?
Thank you guys so much for your help here, because I am completely and totally lost when it comes to this. I have been programming in java (just making .java files in JCreator basically) for about 2 years now.
I have searched and searched but never found a good answer. Anyone that can shed the like on how running .java files as an application and things work, that would be awesome.
P.S. I am using GUI, and the code is correct.
Thank you so much!
What is a runnable jar?
A jar is a archive (it uses the same file format of a ZIP) that contains program artifacts: compiled classes, images, configuration files and other resources.
To be a runnable jar it must contain a special file, called manifest (the manifest support electronic signing, version control, package sealing) with a Main-Class: project.name.EntryPoint entry that specifies the class that is the start point of the program.
This can be easily achieved by using the Eclipse feature File > Export > Java > Runnable JAR file.
How to run a runnable jar?
Using the command line, just do java -jar <jar_file_path>.
But nowadays the graphic shells of the operating systems (Windows Explorer / Gnome / KDE) already call java when you double-click a jar file.
How to put images on the .jar?
You can do this specifying on the Eclipse project that the images folder is also a source folder. Now when exporting the project to the jar file it will include them.

Categories