Cannot open java programs I saved in eclipse - java

I had no problem running my java code in eclipse last week and I have no problem creating a new java application in eclipse. However, I am unable to open any of the java program files in eclipse that I saved. I checked to see if my jdk se development kit was removed from my computer by mistake and it is still there. What do you think is wrong? Why can't I open my old files? All of my java programs are saved as CLASS files.

Source files are normally saved with a .java extension. Once you compile your source files, javac will generate you some files with a .class extension. These are not source files, and are usually deleted and recreated every time you compile your code. If you want to find your source code, you'll need to look for the .java files on your disk.
If you've deleted your .java files by accident, it is usually possible to decompile the class files into something resembling the original source, but much of the original formatting will be lost - comments etc. This approach is far from ideal, but may help you recover the situation if you cannot recover the original source files. A good decompiler can be found at http://jd.benow.ca/ - you can either download the standalone application, or it has plugins for Eclipse and IntelliJ.

Hi once you compile the source java files .java extn the class files will be created. Check your workspace in your disk there will be a folder as package as you created in eclipse under src folder.

Related

vscode debug pauses on decompiled .class file instead of source .java files

This has been bothering me for quite some time and i have yet to find a solution.
When I set a break point in VSCode for debugging java the debugger pauses on the .class files located in the maven target/classes instead of the .java file.
I would like it to use .java just like the way Intellij does it. Its really annoying for me when I find my bug and edit the code then hit replay only to find I edited the code in the .class file instead of the .java. The .class file gets overwrite on the next debug play and you lose all the changes you've just made.
I have found this link below on github that asks the same question but there is no answer
https://github.com/microsoft/vscode-java-debug/issues/915
I am using the Microsoft java extension pack.

How to change the CodeRunner .class file creation from same working directory of the java file in vs code?

I am currently using vs code to run my java files.
But when I use coderunner and run my java file it creates a .class file for every class in the same directory of my java file. Which gets messy how can I get rid of that?
I am ok with uninstalling the coderunner but in that case the java file errors doesn't show up properly and build fails.
Uninstall Code Runner and choose Java: Create New Project from Command Palette.
Create a no build tools project and coding in .java files which stored in folder src, then all the generated .class files will be stored in folder bin by default.
About the compilation errors, please post code snippets for further solution.
Install Test Runner for Java or Extension Pack for Java extension both by Microsoft. Better would be to go with an extension pack.

Why does VSCode keep opening *.class files as opposed to .java files?

(I couldn't figure out how to upload my screen capture to stackoverflow. So this is a streamable link: https://streamable.com/0im8tx)
In this video, VSCode opens QueriesController.class as opposed to QueriesController.java when I cmd click into QueriesController.
I have compiled provided the definitions of the jar file in my workspace:
"settings": {
"java.project.referencedLibraries": {
"include": [
"<path-to-jar-that-contains-QueriesController.jar>",
....
"sources": {
"<path-to-jar-that-contains-QueriesController.jar>": "/my/local/java/definition/src/folder",
Does anyone know why VSCode is choosing to open the definition as a .class file rather than a .java file?
I use commands to generate a simple jar package and use it in another project. It's true that when we click the class name, .class file is opened instead of .java file:
About how to generate a executable jar package, you can have a look at this reply:
Compile .java file and generate .class;
Generate manifest and pack them into jar
In general, a JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.
.java file isn't included in jar packages, and that's why you get .class file opened instead of .java file.
I am not familiar with VSCode but your problem is common across most IDEs.
Usually when a jar is made, it consists of compiled class files rather than original source codes. The reason for this is to run code as efficient and fast as possible and usually people don't want source code in jar because when running they also have to be recompiled again which is a waste of time.
Take a look at this picture. I have just downloaded a jar file from mavenrepository and it downloads the compiled version of jar. The extension is .class
What the IDE does is it tries to decompile the code with a decompiler (In this case as you can see FernFlower decompiler).
However it lacks formatting and in-code documentation the source code (.java) has. Which is why most IDEs offer to download sources. Intellij shows this right on top. Other IDEs may have this setting buried in deep. (You may have to check for yourself)
When you download sources, IDE try to contact the server and download original source code. Probably that would look something like this:
If you look closely you can see name has changed to .java which represents the source code.
VS Code has option under Java Settings, Java Download sources and Maven download sources.
It is not enabled by default. Upon enabling it, VS Code shows the proper source file, although the name appears to be .Class files.(Upon Ctrl + Clicking the symbol, with method implementations, comments, etc.,JavaDoc Comments)
If proper sources are not found in m2 repository, it shows the decompiled class file with stubbed methods. A comment similar to this is shown at the beginning of the file.
// Failed to get sources. Instead, stub sources have been generated by the disassembler.
// Implementation of methods is unavailable.
In Either of the cases, VS Code shows the maven library files as .Class files in read-only mode. Also, source files are not displayed on the Java Project Explorer.(Although even if it exists in the local .m2 repos).
Hope that helps! Happy Coding!

Extracting java class files from jar

I had running executable jar of a Java project. Unfortunately I have lost all the source code but I still got the executable jar of it. Is there any possibility to extract my classes from the jar?
I have tried extracting but class files are in damaged shape.
To get source from .class files , just download a decompiler from here and get them.
Are you talking about decompiling or just receiving the class files? The class files can be extracted using any software capable of reading ZIP. If you talk about decompiling that usually will work purly. But you could try Jad which I had the best experience with.

How does one build the java JRE from source (src.zip in JDK)?

Surprisingly enough I couldn't find the answer to this question.
I am trying to rebuild the java JRE from source. I obtain the java JRE source by extracting the src.zip file in the JDK.
After making any changes I need to make to the JRE, how do I compile the new source back into .java files (after which I can compress it into the rt.jar file).
Thanks.
You have better chances using OpenJDK (the base of Oracle/ Sun's future JDKs).
http://openjdk.java.net/
But what do you want to change actually? Maybe there is a better way...
Some of the Java sources that make up
rt.jar are generated during the build
process, from scripts and other means.
Some of the properties files are also
generated this way, and some of the
properties files are turned into Java
source that also contributes to
rt.jar. So without doing a complete
build first and populating the
'gensrc' directory, you won't have all
the sources that make up rt.jar.
Taken from:
http://www.java.net/forum/topic/jdk/java-se-snapshots-project-feedback/it-possible-just-build-rtjar
So when you say javac on all the java files inside src.zip it won't compile as the dependency graph is broken (missing generated files)
Also have a look at this: Where to get full source code for rt.jar?
If you want to change a number of class, you only need to compile those classes. You don't need to compile the whole JDK unless you intend to replace it.
If you just want to patch it, create a JAR of your changed classes and add this to the boot class path.
After revisiting the question. Javac on any of those files will allow you to rebuild them. Also you don't compile .java files into .java files they become .class files. You can write an ANT build script to handle the heavy work for you.

Categories

Resources