Eclipse: displaying code source for class file? - java

I'm running Eclipse 3.5.2 and am debugging a class which calls a method -- let's call it flaky() -- in one of my other .jar files. When I step into flaky(), it shows the source code in a tab in the debugger, with the filename as Flaky.class.
The problem is, the code I see looks old and out of date, so I'm wondering which file I'm looking at.
Is there any way to find out the path of the file Eclipse is displaying?

Go to the Package Explorer view with the file open in the current editor. Click the "Link with Editor" toggle (the two arrows). This should jump you to the class file you are viewing, which should be in the jar you are using. If the jar is in a library, you should see the path to the jar in the package explorer. If not you can see the path to it in the preferences. From there you can also use Chin Huang's answer to find where the given source file is.

View the Properties for the JAR file. Under Java Source Attachment, you will see where Eclipse gets the source code for classes in the JAR file.

Related

A text file is not read by a Java program in NetBeans 7.4

My Java program only reads a text file fine if its absolute path is indicated as it appears on the file system, f.ex. ="C://textfilename.txt", but does not read it if its name is indicated as "../textFilesFolder/textfilename.txt". The Java program is located in a folder at the same level in SourcePackages as the textFilesFolder.
Is it necessary to configure something in NetBeans for this to work? Thank you.
If you go to your project Properties, then go to the "Sources" category, you can "Add Folder" to your "Source Package Folders". Once you do that, the new directory and the resources in it should be visible in the Projects window. See if that helps.
If not, I wonder if the Working Directory (in the "Run" category of the same project Properties menu) for your project is set incorrectly.

Eclipse - Source not found

I know this question has been asked many times before, but none of the proposed solutions resolve my issue (or I'm not implementing them correctly).
I'm developing a plugin for OpenFire, and when I set a breakpoint in my plugin source Eclipse reports "Source not found". The JAR is built separate from the build of the OpenFire server. I've tried adding the JAR and specifying the source code directory, but no dice.
Here's the process I'm following: When I hit my breakpoint I'm clicking "Edit Source Lookup Path", clicking "Add", Java Library, User Library, click User Libraries, adding my library (tried both "Add JARs" and "Add External JARs"), and then specifying the folder that contains the source code. I've also tried:
Adding the source by selecting "File System Directory" instead of "Java Library"
Adding it by selecting "Workspace Folder"
In the Project Explorer, adding the source for my plugin to the Java Build Path
In the Project Explorer, adding my JAR as a Library and specifying the Source attachment
Thanks in advance for any suggestions.
Personally, I have no good experience in Eclipse JDT, when adding a source folder at the time it hits a break point. What I always prefer to do, is adding the source folder before debugging:
Select Project/Properties/Libraries
Select your library and "edit..." the source attachment
Select either the corresponding source folder or the zip/jar containing the package
After that, you should be able to open the source files in the virtual "Referenced Libraries" folder (directly below the "JRE System Library" folder inside the project), or when opening a reference to a linked class file from within the JDT editor. If you are able to access the source, then you should also be able to at least stop at the break point and see the corresponding source.
Else, you will have to check again, if the source folder is really valid: The source folder or source archive must contain the folder with the name of the root package of the library (e.g. the default Java "src.zip" inside the JDK folder also includes a corresponding "java" folder at its root). When in doubt, extract the archive and select the parent folder of the package you are interested in - sometimes source archives might be a mess or incompatible to JDT.
If this has been assured and it still doesn't work, chances are, that your linked source folder does not correspond to the compiled version of the library. Usually JDT will handle such inconsistencies fine, but if you try to open a source file, that is entirely different than the corresponding class file, you will encounter problems. In this case I would suggest either downloading the correct source version of the library, or recompiling the library from the source, if all else fails.
If opening a referenced source file from withing the JDT editor does work fine, and you are still unable to open the source files when a break point is entered during debugging, then most likely the class files are missing the line numbers of the corresponding source file. Again, you will have to recompile the library from the source in this case.
Finally, it is also possible to overwrite the default source lookup by specifying source folders or archives in the source tab of the launch configuration. But you should normally not need to do this, when your build path is configured correctly. From the Eclipse Help:
The Source tab defines the location of source files used to display source when debugging a Java application. By default, these settings are derived from the associated project's build path. You may override these settings here.
I had this very annoying problem for a long time but was finally able to solve it. In my case, a null pointer exception was being thrown somewhere in Java's Transformer.IsRuntimeCode(ProtectionDomain) function.
I didn't really need to know about this since the exception was being caught and handled, but eclipse would pause debugging every time this happened and tell me that the source wasn't available. As a result, I constantly had to keep pressing the button to continue code execution.
In order to prevent this from happening, I:
1. Clicked on the "Breakpoints" window at the bottom of the debugging
screen
2. Right clicked "NullPointerException"
3. Unchecked "Caught"
This prevented the debugger from pausing program flow during a caught NullPointerException.alt text
(source: SharpDetail.com)
And this another one as:
Eclipse doesn't crash. You're trying to step into a method, where eclipse doesn't know where to find the source (*.java) files and informs you about this. Here is how to tell eclipse where to look. Go to
Window->Preferences->Java->Installed JREs,select the JRE you are using and click Edit.
There, select all of the jar files in the list you see and and click Source Attachment....
In the window that shows up, select the fille src.zip, which is in your JDK folder (if you didn't uncheck it while installing the JDK). On the machine I'm on right now, that is
C:\Program Files\Java\jdk1.7.0_07\src.zip.
Save all your changes (possibly restart eclipse) and you won't see that error again.
Well it turns out the solution was anticlimactic. When Balder's recommendations did not work I tried debugging one of the stock OpenFire plugins and it worked just fine. I then created a new plugin from scratch, and by doing nothing other than adding the source to the project (Right-click on the project -> New -> Source Folder) it also worked just fine. I have no idea why Eclipse refuses to see the source for my original plugin, but I moved all my code and libraries over to the new plugin and debugging is working as expected.
Many times you put jar files in eclipse IDE as referenced libraries when binary version of application was downloaded. Usually done so by configuring the build path. But the binary does not have the source files. You have Binary version and source version of application.
One simple way is to download the source libraries that you also use maven to build the project.
Keep somehwere, may be inside your project workspace.
Now while seeing the class file (from eclipse, jar exploded) you may see the source not found, fine.... there is a button below and click on that, a new window opens and there select add external folder.
Reference it to the src folder of the source you downloaded(not the binary one) and kept somewhere as said above and it will show the class details from that.
I fixed this issue with doing the following:
Click at the menu Window - Preferences - Debug - Step Filtering And check all the packages like the following image.
(Step Filtering)
Then, debug again your project and thats it.
Best regards,
In my case, I had a breakpoint in the class declaration. I mean in the next line.
public class GenerateInterface implements JavaCall {
So, the debugger stopped in that line and showed the following message:
Source not found
I think unconsciously activate the breakpoint in that line.

Where to find javadoc

I am using eclipse for java. When I debug, 'source not found' came out, is it because of javadoc? Where can i find javadoc in my desktop?
It appears your debugger stepped into a library class, and Eclipse cannot find the source for said library class.
You must tell Eclipse where the source files are located.
This is not related to javadoc
When you debug and when you go to inside of a method to debug and if you call that method through a jar file you will get this "source not found". If you can use source codes instead of that jar you will not get that and it will direct you to required source line in that method.
Eclipse telling you that source is not found has nothing to do with javadoc, but with the actual java files.
Most probably you do not have your project configured has a java project with source folders configured. To configure your project source folders do the following:
Right click on your project's name in the navigator view
Select properties
Go to Java Build Path -> Source
Make sure all your source folders are there.
After making sure that all source folders are correctly configured, you should move on to your debug view, and while debugging:
Right click in the attached process
Select Edit Source Lookup
Make sure your project is in the list, otherwise add it.
It can also happen that Eclipse is trying to display you a source file which is not from your project but rather from some other jar you're using or from the actual Java Runtime.
If that's the case and you still want to debug that source too you have to download the source (the correct version you're using) and use the previous 3 steps in the debug view to add that source to your debug environment.

How to attach javadoc for jar in eclipse?

Is there any way to attach javadoc for struts.jar in Eclipse, like we attach it for core Java which is a zip file distributed under JDK?
For to add the Document to the Library, do the following things.
Project(Right Click)->Properties->Build Path -> libraries ->JRE System Library then expand each api and Added the location of API.
Now select the Library for which you want to add the Documents.
Click on Edit, then Navigate browser to select the Directory of the Documentation directory. Please try with only Directory, not the index.html file.
Right-click on the jar, click "Properties", then fill the javadoc location.
First we need to download the source files for any jar file which we are using.
After that we can attach that zip or jar file to eclipse by using to double click the used java class name and then click on link attach java source and then select respective jar and zip of source file.
It may be that sometimes after attaching the java source zip or jar source file is not opening in that case, you can create jar by using the jar command from command prompt by taking the package which you want to include.And after creating this jar we can attach it through the previous step. The source file would definitely be open.

How to take package .java files into a Eclipse Project

I've got a copy of a java package, with example implementation. The package is in a structure called com.java.project (folders, containing myriads of .java files) and there's also a few example files (example1.java, example2.java)
The example files have 'import com.java.project' calls in them, that's all well and good. However, I can't seem to work out how to get Eclipse finding the package contents (currently returns "Type not found" for any calls to the project objects.
I've tried placing the com structure in the same level as my example1.java, but that doesn't work. I have read that I need to compile the package into a .jar to get it to work w/Eclipse, but that doesn't seem to want to behave, none of the files compile.
In Eclipse you can right click on the Project Explorer and select "New -> Java Project"
Then you can import your java files by right clicking your project, selecting import from file system. If the class file shows an error, open the class file, the class name should have red curly braces, click on the error tick mark and Eclipse will give you an option to move the file to the correct package which in turn creates the folder structure for you.
1.Can you do a check on the build path of your Java project? (Right-click the project -> Properties -> Java Build Path -> Source tab), and see if the root folder contains the code is properly specified here.
2.Maybe a Project->Clean will help as well.
Hii,
Just Create a new project with File -> New menu
you need to open Java Perspective and there you will find a package named "src" you need to copy your "com" directory and paste it at the src folder.
I hope this will work for you.

Categories