Using .class files in netbeans - java

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.

Related

How to make VS Code recognize .class file?

I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there

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!

How to add the classes which are designed by me into java library ? (to use "import" keyword in other classes )

I want to use the classes (that are .java files) which I designed separately in java and I want to add them into java library to use them in other classes with "import" keyword. How can I do this ?
Also, I should explain that I copied the classes folder in the zip file which its name is src.zip at C:\Program Files\Java\jdk1.7.0_25 but when I use the "import" keyword in the IDE it seems that there is not any folder with that name in java library.
I am not sure if I understand this correctly but you need to make a jar file from your library project and then place that jar file into the project which wants to use that library. How do you do this depends mainly on the IDE which you are using.
Add library in NetBeans
Add library in Eclipse
Add library in IntelliJ
What needs to be available to Java are the object (aka .class files). So, you can do 2 things:
Locate the directory where you .class files are and add it to the CLASSPATH (either by changing the environment variable, or by specifying switch -cp to java). Be careful with one thing: if you have a class MyClass in package stackoverflow.com, in the directory structure you will see something like .../com/stackoverflow/MyClass.class. The base folder to include in CLASSPATH is .../com, not .../com/stackoverflow .
Packaging your classes into a .jar file (pretty much a .zip, but with a different extension), and add it to the CLASSPATH as in 1. Note that n this case you use the name of the .jar, not the name of the directory it is in.
src.zip does not contain classes, but source (.java) files. That's why its extension is not .jar, and also why you don't see any effect.

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.

Can i put java files in a folder in a pydev project?

I have a .java and .class file which i put under a folder inside my pydev project in Eclipse (because im primarily using python).
Inside my python script i wanted to call the java class file using os.system.
os.system('java -mx1500m D:\\projects\\socialsense\\src\\ss\\samplefile\\test')
However it says that my class file is not found. What is wrong?
I expect that you can do that. However, most people would probably put the java source and python source in separate directory trees, and also not put .class and .java files in the same tree. (If you lump everything into the same place you'll cause yourself pain when you try to implement a "clean" rule in your build file ... or when you want to check your project into version control.)
Your immediate problem is that you've got the command syntax for the java command wrong. The name of the entry point class is specified by giving a fully qualified class name, not a pathname. And you probably need to use the -cp option as well.
For details on how to do this right, refer to the java manual page.

Categories

Resources