Why do I get the message "source not found" from Eclipse? - java

I'm new to Eclipse. When I debug and enter some function, I get the message
"Source not found" despite the code is mine. How can I solve this issue? Is there an addon that might help?
I get error class not found exception with source not found but when I press for F6 (lot)I proceed with the debugging ,the issue is just when I press F5 and want to dive in to some method code (of mine btw that just on different package)– FedorE

You need to add your source code in the debug configuration:
Perform the one of the steps below:
Option1:
Right Click(Java Source) -> Debug As -> Java Applications
Option2: If option1 doesn't help:
Menu ->Run ->Debug Configurations..-> Java Applications ->RightClick & select New.
In right side,
Enter Name
Browse your Project and the class having `main` method
In Classpath, add your project from workspace
Click Debug button in the bottom
Let me know, if it doesn't help.

That is because , while you are in debug, the debugger enters part of code which does not have attached source files.
what you can do is,
IF you have got the source to that file :
when you see that source not found screen, there would be a Attach Source button. press that, and browse to file that should contain source for that class. and hit ok. eclipse will load the source and you would be good to go.
Sometimes debugger on repetitive step into enters some API classes or plugins whose source you may not have, then your best bet is to repetitively press F6 till debugger exists that class

Related

Weird compile error in Eclipse and Netbeans

I am getting an error : Syntax error on tokens, delete these tokens, on valid Java code
My code, copied and pasted from eclipse, is here :
public class Test2 {
    public static void main(String[] args) {
    }
}
I am guessing that there is some hidden character in the code which I can't see.
I tried restarting Eclipse, and cleaning the project, but the error is still there!
Even if I copy and paste this code from SO into eclipse the error is still there!
what is going on here?
EDIT
Its not Eclipse! In Netbeans I get the same error :
Also, the original problem occurred when I tried to copy and paste code examples from this webpage http://blue-walrus.com/2011/05/advanced-swing-multithreading-tutorial/
I just did the following:
Cut and paste the code from your Question to a file and compiled it using javac (Java 8). No compilation errors.
Opened Eclipse (Kepler), created a Test2 class, copied the code from the Question to it, and saved it. No compilation errors.
My tentative conclusion is that there is nothing wrong with the java code. (No funky hidden characters, no nasty homoglyphs, no messed up character encodings.) The most likely cause of these weird errors is that your Eclipse session is messed up:
Try restarting Eclipse.
Try closing and opening the Project.
Try loading your code into a fresh workspace.
In extremis ... try a fresh install of Eclipse (same version as before ...)
Following on from my comments your project must have a valid JDK and JRE defined
If these are missing from your project, go to Window > Preferences> Java Installed JREs and click Add..
If you make any changes select Project > Clean... again
To remove these strange hidden empty space characters, I copy and pasted into Outlook Email, and then copy and pasted into Notepad++, and then copied into Eclipse. All fixed :)
Seems you need to copy and paste situations like this through a few different apps, to 'scrub' the text of any funny characters.
I think the java class path is not set in your computer if that is the case go to my computer->properties->Advanced System settings->Advanced tab click Environment variables find variable path & check if the java class path is there. If its not there i suggest you to copy the java class path & edit variable path & paste the url followed by ";" & then restart the eclipse.
better to check your project build path i.e, JRE system library version/is that set or not in the eclipse
way to go for properties section of eclipse :
right click on project - > choose properties ->build path->Libraries->Add Library Variable->JRE system library->click Add alternate JRE radio button->and select your machine jre->click finish->OK
after that go for java compiler in the same window (left side)->check the enable project settings->and choose your java version from drop down box->click Apply->OK
finally save your program and run it
if it again fails you need to check whether you are setting the classpath in proper or not, for this
please refer this link to set classpath :Link
ZWSP and other non-printing characters
Means a Zero-Width-Space character. You are using UTF8 and some characters have a width of 0 so you can not see them.
Change the Encoding to ANSI and back will solve your Problem.

In eclipse, how can I find where in my classpath a type is coming from?

Maybe I'm misunderstanding something, but..
I want to be able to see that eclipse is getting Foo.class from a .jar (somewhere in the classpath) when it builds project B.
If I use the 'open type' (Ctrl + Shift + T) it seems that that just shows me all the places that Foo.class exists in my WORKSPACE?
Many thanks..
EDIT: I'm trying to find which jar is introducing a class into my code. It's not my jar. I don't have the source. I can't therefore open it and click it. I want a window that I can type in a type name, like 'Foo.class', and it tells me where that class is being brought in in my classpath. That must be quite simple? Ideally it would also tell me which occurrences later in the classpath were being hidden because it found the first one first. Does that make sense or am I talking nonsense?
EDIT: Guess I'm not making it clear. I don't have a piece of code that uses Foo. It's that somewhere in some included open source library something calls something calls Foo. So I can't highlight or right click anything. I guess I could write a piece of code that has it in? Seems a bit clunky..
Came here looking for the same, so though the question is two years old:
In eclipse Mars, if you press shift+control+T / ⇧+⌘+T on mac to bring up the "Open Type" dialog, the package as well as the name and path of the jar file is shown in the bottom of the window for a selected class.
Alternate Solution : particularly if you want to know the jar files which have needed class , you can search using WINRAR.
Use Find in WinRAR
i) Open WinRAR
ii) Open lib folder ( which contains all jar files ) in WinRAR
iii) Click on Find , type any classname (ex : ClassWriter.class )
iv) Click on OK
you will see all the classes which are named as ClassWriter.class in all jar files in lib folder.
Since there seems to be no good solution or plugin for this, I often create a dummy class in the root of my project:
public class Find {
public static void main(String[] args) {
System.out.println(
SomeClass.class.getProtectionDomain().getCodeSource().getLocation());
}
}
Then I just delete the class. A plugin would be nice :)
Look for .classpath file in your project.
OR
Where ever class Foo is used, go to that statement, while Ctrl key is pressed, left click the class name. Eclipse will take you to the location of Foo class. This will the Foo class eclipse is referring while building your code.
Right click on the class name, then select "Open declaration" (or just press F3), if you don't have a source attached, you can do it at that point. In the package explorer you have the functionality "Link with Editor" (a button with a couple of arrows going back and forward), that redirects you to the jar of the class. If you don't have the source, you still can use the "Link with Editor" functionality to see the propietary jar

Why does the Eclipse debugger give the error "Source not found" seemingly without reason?

When I'm debugging in Eclipse, it often happens that the debugger crashes, displaying the error message "Source not found" (under which is a button with the text "Edit Source Lookup Path"). I have previously searched the web for an explanation/solution to this problem, but found nothing of help to me.
However, I've now figured out what is happening in my case: The error occurs when stepping through the code line by line, and then stepping out of a block of running code. I don't know the terminology, but I guess many applications might enter "standby mode" at some point, where none of its code is currently running. One example is a graphical application waiting for a mouse click. Stopping at a breakpoint in a MouseListener method, and then stepping out of it (into "standby mode") will cause the error in my case.
I've supplied an MWE at the bottom of this question. The error occurs when I place a breakpoint at the line
System.out.println("You clicked!");
and step out of the method line by line using F6 ("Step Over"). If I press F8 ("Resume") instead of F6 at the last line of the listener, the debugger doesn't crash and everything is fine.
My question is: why does Eclipse do something so severe as to crash in this case? I understand that there is no line in the source code that the program control can be said to "step to" after leaving the listener in the below example, but why not just go into "standby mode" without complaining? Can I deactivate this error somehow, to prevent my debugging sessions from so frequently meeting their untimely end? Or do I just have to remember to press F8 instead of F6 when the latter would cause a crash?
package app;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
public TestFrame() {
getContentPane().setPreferredSize(new Dimension(200, 200));
getContentPane().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("You clicked!");
}
});
pack();
}
public static void main(String[] args) {
JFrame testFrame = new TestFrame();
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setVisible(true);
}
}
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.
goto
window->preferences->java->installed jres
and delete the jre, then do "add"
and look for the JDK not the JRE!
The aim is to be able to open a .class file without a source
As the previous answer mentioned the decompiler on the marketplace fixed it for me. After installing, you then have to follow these instructions I found on - Mkyong.com
How to configure file associations
Below are the steps to configure the class without source to Class Decompiler Viewer.
In Eclipse IDE.
Clicks Window -> Preferences -> General -> Editors -> File Associations.
In File types section, select *.class without source.
Scroll down to the Associated editors section, select Class Decompiler Viewer, clicks Default button. If the Class Decompiler Viewer is missing, clicks Add button to add a new Class Decompiler Viewer. The key is to make this the default.
Click Apply and Close button.
"Source not found" error can be resolved by providing the source of the class file i.e .java file so that it can navigate to the source file. Otherwise, you can go for a decompiler to open the .class file directly.
Go to Eclipse-->Help-->Eclipse MarketPlace -->Search for decompiler and
install it.
Now, the issue can be resolved.

Android. Support4Demos crashes

I have runtime errors while executing Support4Demos sample.
I'm trying to launch Support4Demos sample (both on emulator and device). There are no errors in Eclipse and it's launched well. As you know first I should select category (for instance "Fragment"), then subcategory (for instance "Tabs"). And after I've selected subcategory - app crashes with following log
12-27 16:39:51.796: E/AndroidRuntime(384): FATAL EXCEPTION: main
12-27 16:39:51.796: E/AndroidRuntime(384): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.supportv4/com.example.android.supportv4.app.FragmentTabs}: java.lang.ClassNotFoundException: com.example.android.supportv4.app.FragmentTabs in loader dalvik.system.PathClassLoader[/data/app/com.example.android.supportv4-1.apk]
I did't make any changes in the source code. May be I missed something?
Could anyone help me?
You should put the android-support-v4.jar to the directory named "libs" of your project and then refresh the project.
If you add the android-supportv4.jar it will work. Here are some detailed instructions on how to do that:
http://developer.android.com/tools/extras/support-library.html#Downloading
You need to follow every step to a "t" in that link. However, sometimes the instructions
are not very clear. So, I will try to explain what some of the instructions mean:
I found the .jar file which I needed to add in the following directory:
C:\Users\nadir\android-sdks\extras\android\support\v4
the steps say, In Eclipse, right-click the JAR file in the Package Explorer, select Build Path > Add to Build Path
Let me show you what that looks like on my computer:
the instructions also say: select Build Path > Add to Build Path, you may not find it worded exactly like that. Here is what I did, i(note: I clicked on the "add external JARs" button):
Be sure that android-supportv4.jar is in your classpath.
The jar is located under /extras/android/support/v4/android-support-v4.jar

Eclipse autocompletion problem

Just installed Eclipse Helios (Win7 64) and I'm having a strange problem. I type syso-Ctrl-Space which is expected to complete to System.out.println("") but it doesn't work! I searched the web for about an hour now, I set Preferences > Java > Editor > Content Assist to defaults, nothing. Everything else seems to work. Ideas?
To be clear: the shortcut works, it completes everything except "syso" as seen so far, on syso (also tried sysout) it says "No Default Proposals". I couldn't find this kind of shortcuts in the Content Assist, but I set them to default anyway.
If you still cant get auto-completion working,try enabling the Template proposals under Content assist menu.This is the exact location,
Windows->Preferences->Java->Editor->Content Assist->Advanced
And make sure you have only one entry for syso(Under Templates menu).
I believe the right template is "sysout" and I believe this can be configured somewhere but could not tell you where. Try sysout and then Ctrl+Space
It's a template, you can find all templates under
Window -> Preferences -> Java -> Editor -> Templates
There you can see that the template name is "sysout" this will then trigger the string substitution for System.out.writeln() but you can change both the template name (if you prefer syso) and/or the actual code produced by invoking the template
Cheers
I am using Eclipse Galileo and had the same problem. Even after trying the above proposed solutions but it didn't work. After making the below change it finally comes out.
Go to Windows->Preferences->Java->Editor->Templates
Select sysout template and edit it.
Change the context from Java statement to Java.
And it worked for me. I hope this will be helpful for you.
As Peter stated, it is under Window -> Java -> editor -> templates.
Look for the line with the following values.
sysout "Java Statements" "Print to standard out" "on"
the preview should be as follows...
System.out.println(${word_selection}${});${cursor}
Your cursor needs to be located in a place where it would be legal to get that line. If your cursor is within a method definition and you type syso you will get the replacement. However if your cursor is in the portion of the text outside of any method definition, say where you declare variables for your class for instance, then syso will not suggest the illegal line system.output.println();.
I had this problem, read all the answers here, became convinced that it MUST be working in my copy, and realized I had been typing syso BELOW the closing } of my last method instead of in the method.
Eclipse on!
You may try to set the shortcut of Content Assist with ALT+L, if it works, then you can choose your favorite shortcut again. (The shortcut Ctrl+Space conflicts with the shortcut of switching input method). In eclipse on linux, the shortcut of Word Completion is ALT+/, so if you set this as the shortcut of Content Assist it also conflicts.)
Just go to the below address:
Windows->Preferences->Java->Editor->Content Assist->Advanced
and click restore default..........that's all
its work like a charm again.
Also check in this eclipse menu:
Windows -> Preferences -> Java -> Appearance -> Type Filters
If you see java.* or javax.* here then try removing it (or Restore Default will do the job) and see that works, it did for me.
Just found the solution for Eclipse Luna: write syso and press CTRL+Space and click on "enable intelligent code completion" and you are set.
I am sure you can find it somewhere in the options as well but this way it is fast and easy!
DIRTY WORKAROUND:
You can try out the following AutoHotkey-Code if you're still struggling like I am (STILL haven't been able to fix it myself)
#persistent
#ifwinactive ahk_class SWT_Window0 ; eclipse java
:*:syso::
sendraw System.out.println()`;
loop, 2
send {left}
return
#ifwinactive
You need to write this in a .ahk file and run it (AutoHotkey needs to be installed). You might wanna put this file into your startup folder. It's a very useful tool, you might wanna expand it one day.
You might have overlapping shortcuts.. Goto 'Overview' than the 'Keys' subsection. Type "ctrl +space" in the 'filter text' section. You should see the "content assist" and probably another command with the same shortcut. If another shortcut does exists, change its shortcut to something else.

Categories

Resources