Reputation: 13499
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/
Upvotes: 0
Views: 731
Reputation: 1974
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.
Upvotes: 0
Reputation: 13499
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.
Upvotes: 1
Reputation: 15879
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
Upvotes: 1
Reputation: 107
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
Upvotes: 0
Reputation: 718758
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 ...)
Upvotes: 1
Reputation: 1
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.
Upvotes: 0