How to step onto/debug imported java code in netbeans - java

I have been searching for a long time on how to step through imported code classes in netbeans with no solution.
When debugging or stepping into the code, netbeans does not highlight and stop at the imported code classes being used. This is extremely inconvenient since the code I am working on is complex and stepping through is the only way to fully understand it.
I created a new project with existing sources. I have made sure that under window>debugging>sources I have added my working folders.
I also made sure the sources were added in the "runtime" classpath.
The compile classpath appears to be empty, I'm not sure whether that has something to do with it.
MORE INFO: In the debugging window while the program is paused, the main method profile from my imported class says it is reading from "Hidden source calls", but it is really just the normal imported code.
Why is this happening? Can anyone please help?

Folks,
One of the reasons you can get Hidden Source calls is adding in existing packages and java source code at the wrong directory level. You MUST add in the parent directory of the package not the child directory that represent the package name,
example if you have /home/torsi/java/src/torsipackage/com/main.java, you need to add in src, not torsipackage, you will now you made this mistake if you see listed in the top-level package name for your package.
Tom

Well I've not heard the term "Hidden source calls" before but I wonder if you're trying to step into third party code? If you want to step into compiled code it must have source associated I believe. I think it's possible to step into native code because they include the source somehow in the distribution for that exact reason. If you want to step into a library that's been developed by some other private entity you will likely not be afforded that luxury unless it's open source, and even then you may have to retrieve the source first.
Ultimately my point is that there is a difference between a .java file and a .class file. I believe it's possible to reverse engineer .class files but haven't done research on the topic.
If you want to step into code you didn't write odds are you'll need to change your code to work with the imported library or alternatively file a bug report with the developer.

Related

IntelliJ IDEA - Decompiling/Editing/Recompiling JAR

I realize this may seem like a completely stupid question and this question is a "wall of text", but I'm feeling like I've gotten way out of my depth here and need some guidance. I've read several SO questions on this topic, but still can't seem to find one that addresses this particular situation. (please reference update(s) at the end of this post)
BACKGROUND
Our company has an application that was built in Java and released as an executable JAR package by a developer who passed away a couple of years ago. Since then, there has been minimal need for even looking at that code, let alone making any changes. That's been really good because I do my programming in VB.NET (Visual Studio) and, while I can read and make sense of Java code, I'm not proficient in actually writing/debugging that code.
However, a recent issue has come up that forced me to try to take a look at this Java code due to some internal changes in organization and data structure. I've looked in his "src\" directory and found older versions of his original code but wasn't able to find the most recent version anywhere. I found some code that made it possible for me to extract the JAR that's currently being used to a local directory on CodeProject (JarsVB), so I've been able to look over some of the .java files when trying to figure out what query is being used for some random operation. I don't know what tool(s) the original developer used to create the project/JAR, so I've installed the IntelliJ IDEA Community Edition 2018 as an IDE, and it's worked for me so far to simply view the code so I can understand a bit about what it's doing.
PROBLEM/GOALS
Unfortunately, now there is a need for me to make a change to the Java code and update the JAR, and this is where I can't seem to make heads or tails of what I need to do. I have my local copy of the code from the "unzipped" JAR containing all the .java and .class files (along with all the other resources), but I'm not sure how to go from that to modifying the code and recompiling the executable JAR.
So, my goals here are as follows:
(properly) Decompile the existing executable JAR. (If the JarsVB solution I mentioned above did what it was supposed to do, I should already have this part handled, but if there's a better, more "integrated" way of doing it, I'd be open to that as well.)
Modify one or more .java files. (I believe I can figure out the syntax well enough to get this part done.)
Debug/test my changes.
Recompile the changes into an updated executable JAR for release. (THIS is where I'm experiencing the most confusion.)
WHAT I'VE DONE
I've been looking at the documentation for IntelliJ to try to figure out how to make this all happen, but I feel like I'm missing stuff. I set my "Project Structure" settings to point to a specific folder, but I can't seem to get it to actually build anything in my specified path.
I went into one of the .java files and made/saved a small change to test, then went to the Build menu and tried all the building options available to me: "Build Project", "Build Module", and "Rebuild Project". Each time, the event log shows that "All files are up-to-date" (even though I changed one), so I go to my output directory to see what it built, but the folder is empty.
I looked at the IntelliJ documentation for Packaging a Module into a JAR File, which says to
On the main menu, choose Build | Build Artifact
But that option is disabled in my Build menu, and when I look in the Project Structure dialog, the Artifacts section is empty, so I try to add a new setting (Add -> JAR -> From modules with dependencies...), but I'm not sure how to properly set that up either.
I tried to select a Main Class from my classes/ directory, but it wouldn't actually accept any of the .class files I selected, so I just left it blank. Then, I did a search for a MANIFEST file, but couldn't find one anywhere so I left that blank as well. The Build menu option is enabled now, but when I tried to Build Artifact, again, I get nothing in my output directory. In fact, the only files I can find that have changed are in my local working directory. I'm just dumbfounded at this point.
FINAL THOUGHTS/QUESTIONS
I've tried to provide as much detail here as I could think of about all the things I've tried and gone through to get this JAR updated, but if there's a question about anything, please let me know. I'm not looking for a "handout" and I don't expect anyone to do this for me, but I'm also not wanting to become a Java developer just for the sake of making some minor changes to an application that will eventually be replaced by a .NET application. I simply am not familiar enough with the tools or Java development in general to know how to get to where I want to be from where I am.
My decompiled source files are in a totally separate directory from the original, production JAR file because, when I recompile this time, I want to completely recreate the JAR. Am I understanding the Java development process correctly in editing one of the .java files I got from decompiling with the JarsVB and then recompiling the JAR?
Thanks in advance for any assistance in finding the resources I need to accomplish these goals.
EDIT/UPDATE
So, looking at the link in the accepted answer in another SO question, How to build jars from IntelliJ properly?, I figured out at least one part of my issue: Leaving the Main Class setting of my Artifacts configuration blank was a problem. Since I hadn't been able to select a .class file and I wasn't sure how to correctly populate that field, I hadn't given the IDE enough information to operate correctly. I entered the namespace and class (namespace.class) I found in the class file that apparently defines the main method for the application, then set the path for the MANIFISET.MF file to my output directory and tried again to Build Artifact.
This time, at least, it DID produce a JAR in my defined output directory, but it's only 1KB in size. As stated above, the source files are in a completely separate directory from the original JAR file from which they were decompiled. The output directory, too, is completely separate from the location of the original JAR file. Do I need a copy of the original JAR file in the output path for recompiling to work correctly?
I'm making progress, but I'm sure I'm overlooking something "stupid" at this point that's primarily due to my unfamiliarity with the IDE and developing Java applications in general.
UPDATE 2
Looking at another SO question - how to create jar of java package in intelliJ idea - I learned that I have to individually add the necessary files for repackaging into the JAR. However, this brings up the question, what files do I add? The linked question identifies the .class files, but when I go look at the .class files in my working directory, none of those have been updated recently, so it looks like I'm still missing a step somewhere.
Another question this brings up is, are there certain conventions for Java development of which I need to be aware when preparing my environment? I have my output path set to a completely separate folder than any working or production code, so I'm wondering if something in that setup might potentially cause issues.
As I said before, I made a small change to one of the .java files, then tried both the Build Module and Rebuild Project options, but those are still telling me that "All files are up-to-date". Even so, I tried adding just the .class files from under my classes\ directory to my Artifact configuration and tried again to Build Artifact. I got a bit larger file (approx. 5MB), but when I try to execute the JAR, it just doesn't appear to do anything, let alone actually launch the application.
I tried again by adding the root folder of my local copy of the source adding everything the root folder contains. (yes, the directory probably needs some "spring cleaning", but that's for another day)
This time, I got a much larger file this time (approx. 21MB), so I thought I might have fixed the problem. Unfortunately, no such luck. The JAR still doesn't appear to execute.
For reference, the original JAR file from which the code was decompiled is approx. 59MB in size so, either IntelliJ is doing an incredible job with compression, or there's yet another step I haven't yet found. I'm sure this is all a matter of getting my IDE configured correctly, but I just can't seem to find the right combination of settings.

Eclipse not implementing .class files that are provided, and not able to super those classes in the project

I have scoured this website for the answer, but none of the solutions I have found worked for me. Please mark as duplicate and paste link if you know that post is the answer.
Anyway, what is happening is I am using Eclipse for my Java project. My professor emailed us classes that he coded and wants us to use for part of the project. I have not been able to figure out for the life of me how eclipse imports these .class files into the project. I have tried several solutions from other posts like linking the folder with the .class files, and none of that worked.
The reason I know it does not work is because I am supposed to be able to do "extends MessageState" etc. (MessageState is one of the .class files he sent us), and yet I get an error that Eclipse doesn't know what that is referencing (because it thinks it does not exist). The classes he sent are all super classes of my subclasses that I have to code myself in the project.
Let me know any ideas you have and I will try them out.
"EDIT"
The link provided was the first thing I tried, and it does not work. I still am getting an error in my class that extends one of the classes he provided to us. I need to use what is in his superclasses (.class, not .java) for my subclasses (.java) that I am currently writing. Here is what it looks like:
public class ServerBeginState extends MessageState
The error is on the "MessageState" which eclipse says: "MessageState cannot be resolved to a type"
This would mean that MessageState doesn't exist and needs to be created, but for some reason I am not linking the classes to this project correctly. That is what I am asking the question for.
So i just decompiled the .class files and made them into new .java files. That seemed to have worked, but if someone finds a way to use .class files for implementation in eclipse (because i know it works in Textpad 8) please post an answer.
Simply Create a new java project.
Then drag and drop the classes sent by your Professor in the folder named source code or "src" .
Eclipse will produce a pop up window with 2 options make sure you select "copy files"
if that does not work try manually copying the files in your src code location which will be found in your java project folder.

Problems with the SonarQube Directory Tangle Index

We're using the latest version (5) of SonarQube to analyze our project. I have some questions about how it calculates the Directory Tangle Index (formerly Package Tangle Index) that I haven't been able to find answers to anywhere else.
Why does SonarQube need to access class files to calculate the tangle index? What information does it need for that analysis that is not include in the java files?
When specifying the class-file location for above, why do we have to provide such a deep path? I want to specify "project.sonar.java.binaries=/path/bin/" not "...=/path/bin/com/company/project/a/b/". I would also be fine with "...=/path/bin/**/*.class" or some other wildcard. To that end, do I need to specify a separate class-file location for each package in my project? The issue we are having is that we are trying to generate the SonarQube config file via an automated process, and drilling down to find all the necessary paths is a problem--the information needed to do that is not available when the process runs.
Why, when I click on the link for "Directory Tangle Index", do I get a page stating "No DSM data available for the component"? I read somewhere that for complex projects SonarQube can't provide the information on which directories are tangled. Why not? Or is the problem that we are only specifying one class-file path for each project, not listing all the separate subdirectories?
The tangle index sounds interesting, but if I can't drill down to which packages are showing tangles, then it won't be as much help for improving our code.
So there is in fact three questions in that question :
This is mainly due to historical reasons. Semantic analysis in java analyzer is quite recent and previous to that it was really hard to be 100% sure of a type refered in a class from the sources (let's say that there was no tool to exploit the data). So the solution was to rely on an analysis of the bytecode.
sonar.java.binaries needs to point to the directories containing your .class files, but it is expected to be at the root package, because the look up for a class will be done assuming a package is a directory from one of the directories specified in the property. (so if sonar.java.binaries is defined to point to path/bin, for class com.mycompany.A we will look for path/bin/com/mycompany/A.class file).
This would require some more information to be able to help in details. (like the source of your statement).

Importing the entire AOSP inside eclipse

I've downloaded the full AOSP source for my device, and was trying to import it inside eclipse to make some changes to the framework (basically it includes an alertDialog that shows when a button in the system bar is pressed and i want to edit it).
I've followed the guide on the AOSP Documentation, and also imported in my build path the android-common_intermediates/javalib.jar but i still get some errors, to be specific I get errors with some fields that should have been defined in one R.java file and I have no idea of how to find the proper R file, i even doubt it's there, maybe is it generated in a second moment or is it in another path and then my compiler script moves it to the right path?
Presuming you heed the warnings in the comments above and still want an answer to the question, I may be able to help. I frequently see similar problems, when I use eclipse to view Android source. There are several projects that are, apparently, not built in response to "make" but that are still included by the .classpath file. My solution is to make them by hand.
Once you've sourced build/envsetup.sh, the command "mm" will build the project in the directory you are in. I start eclipse, find the projects whose resource definitions are missing, cd into them one by one, and use "mm" to build them.
I believe that you will find that they are all projects in the "packages" directory.
So far, this has always worked, for me.

Android: Obtain Java source code from jar

I have a jar library that I use for my app and I want to make changes to the source code.
After using JD-GUI to obtain the source code from the class files, I put the files in my project in Eclipse. But the source code contains many errors. For example, it says that the constructor does not exists, but the source code clearly has this constructor.
Please provide help. Thank you.
First thing, its never advised to change any built in jars which are provided for developers help.
Secondly, if you do want to make changes to it, use JD-GUI, take out the complete source code into a project, know the code, make the changes, compile the program to make sure all the dependencies before and after changes made, is ok so that no errors pop up.
Take the class files from the project and form the new jar and use it.

Categories

Resources