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

(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!

Related

Usage of jar with .java files and odd behavior of the compiler

I was curious about the differences between .jar with .class files and .jar with .java files. I partially got the answer here, But then what is the usefulness of .java files in the jar?
My guess is that the java files in the jar are like an interface that prevents compilation error, because I solved the IllegalAccessError thrown on runtime by replacing jar files with .class with jar files with .java specifically when using Xposed Framework. (Got the hint from this thread.)
Also
Thank you for your explanations and they were helpful. But I want to learn more about the differences in compiler's view, because I am wondering why my app works fine even if I only included the jar with java files, not class files (zxing). Also there are some cases that throws IllegalAccessException when I include the jar with class files, but not thrown when I include the jar with java files(xposed), even though I have to include at least one of them to make the compiler(AIDE) not complain about references, like unknown package. Why does the compiler not complain when I include only jar with java files though the compiler would not be able to resolve the actual implementation of the referred classes?
A .jar file is basically just a .zip file with another extension.
A .jar file with .class files have a special purpose and may have special meta-data (e.g. in META-INF folder).
A .jar file .java files is just a .zip file.
It is however common for open-source libraries to provide 3 .jar files:
One with .class files, to be used by your code, both to compile and to run your code.
One with .java files, to be used by your IDE, so you can drill into the library code and see it. Especially useful when stepping through the code with a debugger.
One with javadoc files (.html files), to be used by your IDE, so you can read the documentation about the classes and methods in the library. You do read the documentation, right?
None of those 3 files have to be named .jar. They could be renamed .zip so you could easily open them in your favorite Zip utility, or they could be renamed .foo just because...
They should be named .jar, to clarify that they are Java ARchives.
Its simple - *.java files are sources, *.class files are compiled classes.
What is used on runtime by JVM?? *.class files. Why would you put source files inside library? IDK, usally sources are distributed as separate jar, but all in all it is done to allow you to check library code without decompilation.

Using .class files in netbeans

I'm trying to use a .class file I downloaded for a uni project. I don't have the .java file to go with it. I am using netbeans.
I tried just adding it to the project src folder
I tried using "add JAR/folder" on libraries and adding the directory containing it
I also tried creating a JAR file of the directory containing it and adding that
Greatly appreciate any suggestions
You should do two things:
Create a jar of the class file you received
Create an overview which methods the classfile offers
For the latter you have at least two options. One is to use a decompiler (some authors of APIs deny you to use this) like JD-GUI. The second options is to use javap, which comes with your JDK (I link to Java 8, but it exists in prior versions too). Simply call javap yourfile.class and you will see which method signatures the class offers.
But the easiest way to see the classes / methods inside the .class file is JD-GUI, so if you are not running into any legal issues use that approach.

Cannot open java programs I saved in eclipse

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.

RobotFramework cannot import Java keyword library

I did some robot framework python examples with pybot, and referenced .py files as my library files. The folder structure I used was test/lib/myLib.py and test/test/myTest.robot, where /test was at the same level as the /src folder in my project in eclipse.
I then tried to do similar things with a java project using jython. I have /test/test/myTest.robot which imports the library ../lib/myLib.java. I even tried importing this file using RIDE, but it always shows up as red text, meaning the import failed. The specific message I get from using jybot on the command line is:
"Importing test library 'C:\Users\cody\git\myProject\test\lib\myLib.java' failed: ImportError: No module named myLib"
I read that I might need to add it to classpath, and I think in order to do so, I need to make it a .jar file. I'd rather not do all that if possible to just leave it as a .java file. I attempted to add the lib folder to the build path... By that I mean I added the /test/lib folder to the "Source folders on build path". I also exported the darn thing as a jar and added that as a library. All this was done from the project properties in Eclipse.
I'm pretty stuck... any ideas how to get this working?
Java being a compiled language, you need to compile your java Class before importing it with RobotFramework.
Normally, Eclipse would do that for you, and put the resulting .class files inside a bin repository. If not, you can issue the javac path/to/file.java command, and move the resulting .class file where you want it (somewhere referenced by the classpath.
From within the .robot file, you should have the line Library test/lib/myLib, without neither .java nor .class at the end.

Convert .jar file to JAVA DOC

How can I put my jar file to web. i.e is there a software that decompile jar file and make html pages of it?
In other words I want to make a java doc.
[Edit according to user comment]
So you want to extract javadoc from a jar...
First you must understand that if your jar doesn't contain the sources, but only the compiled code that your javadoc will not show any comment.
Then you just need to extract the file in your jar using any zip program (for exemple on windows, rename file to .zip, and extract it).
Last thing to do, is to call the javadoc tool on it. Like other said you can use an IDE for that, or simply call from the command line :
http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/javadoc.html#examples
javadoc -d \home\html -sourcepath \home\src -subpackages java -exclude java.net:java.lang
Creating Javadoc the way it was intended works, as far as I know, only from sources. If the JAR file also contains the source files, then it's just a matter of writing a Java program that opens the JAR file and puts any source files through the javadoc utility.
Interesting idea, but I don't know of anything doing that at the moment. To my knowledge it is however possible to generate Javadoc from inside of java program.
Another approach would be to load the classes into your classpath and use reflection to figure out methods, fields, etc. It will give you a rough overview of the classes, but sadly not the detailed stuff "normal" javadoc generation gives you. A lot of information is discarded upon compilation.
Converting .jar file to html seems impossible, except if you intend to users to download the .jar file from the web. However, html to .jar is possible.
I am certain that only the java source can be converted to javadoc. See here to convert java source to javadoc.
I have created a JAVADOC from a jar file that is not created by you i.e you don't have its source code.
Its simple but tricky.
Get source code from .jar file by using java decompiler. I use JD-GUI | Java Decompiler
Make a new project in myEclipse and copy the 'source code' in source folder.
In project menu you will see Generate Javadoc. Click and select you project and follow the steps. Java doc will be ready after you finish it.
Cheers
Imran Tariq
Once you downloaded your APK file , You need to do the following steps to get a editable java code/document.
Convert your apk file to zip (while start your download don't go
with save option , just go with save as and mention your
extension as .zip) by doing like this you may avoid APKTOOL...
Extract the zip file , there you can find somefilename.dex. so
now we need to convert dex -> .class
To do that, you need dex2jar(you can download it from
here , after extracted, in
command prompt you have to mention like,
[here] (Keep in mind that your somefilename.dex must be inside the same folder where you
have keep your dex2jar.)
Download jad from
http://www.viralpatel.net/blogs/download/jad/jad.zip and
extract it. Once extracted you can see two files like "jad.exe" and
"Readme.txt" (sometimes "jad.txt" may there instead of "jad.exe", so
just rename its extension as.exe to run)
Finally, in command prompt you have to mention like [D:\jad>jad
-sjava yourfilename.class] it will parse your class file into editable java document.
Use mvn javadoc:javadoc in Maven.

Categories

Resources