Weird compile error in Eclipse and Netbeans - java

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.

Related

The declared package "" does not match the expected package

I'm able to compile and run my code but there is always an error showing in VSCode. Earlier there was a pop up I don't remember what and I clicked "apply globally" and it's been this way since.
Output is there but so is the error
The declared package "" does not match the expected package
Thanks to the comment of #beatrice above, I found the answer here.
My .vscode\settings.json file had some paths explicitly added. I had added them manually when Visual Studio code was not misbehaving, by right-clicking on certain project folders and doing "add Folder to Java source path".
The solution for me was to basically remove all of them from .vscode\settings.json, replacing its contents with:
{
"java.project.sourcePaths": [""]
}
This finally made VsCode work again.

Editor does not contain a main type in Eclipse [duplicate]

This question already has answers here:
Error: Selection does not contain a main type
(24 answers)
Closed 4 years ago.
I downloaded eclipse-jee-kepler-SR1-linux-gtk-x86_64.tar.gz. This eclipse is built-in with java and my Lubuntu is 64-bit. Whenever I compile and run a simple code in java like this one below:
public class Sample{
public static void main(String[] args){
System.out.println("YOLO");
}
}
I always get an Editor does not contain a main type.
I put the file in a project folder called Sample. This eclipse should compile java code because its an IDE distribution specialized for java.
How can i resolve this error?
Any help would be much appreciated.
Here's my project structure:
Problem is that your folder is not identified as a Source folder.
Right click on the project folder -> Properties
Choose 'Java Build Path'
Click on 'Sources' tab on top
Click on 'Add Folder' on the right panel
Select your folders and apply
I just had this exact same problem. This will sound crazy but if someone sees this try this before drastic measures. delete method signature:
public static void main(String args[])
(Not the body of your main just method declaration)
Save your project then re-write the method's header back onto its respective body. Save again and re-run. That worked for me but if it doesn't work try again but clean project right before re-running.
I don't know how this fixed it but it did. Worth a shot before recreating your whole project right?
I suspect the problem is that Sample.java should be in a package inside the src folder.
I guess that eclipse will not automatically look outside of there.
Right click your project > Run As > Run Configuration... > Java Application (in left side panel) - double click on it. That will create new configuration. click on search button under Main Class section and select your main class from it.
First look for the main method is there or not.If it is there, do restart your eclipse and right click on the page which having main method, Go to run as Java application.
Make sure you do Run As > Java Application.
If not you could try a Project > Clean
Some more questions that deals with this that could be helpful,
Refer this
Create a source folder under Java Resources
For me, classpath entry in .classpath file isn't pointing to the right location. After modifying it to <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> fixed the issue
Ideally, the source code file should go within the src/default package even if you haven't provided any package name. For some reason, the source file might be outside src folder. Create within the scr folder it will work!
Right click on Sample.java file and delete it. Now go to File -> New -> Class , enter name of program (i.e. hello) , click on finish . It will create file hello.java. Enter source code of program and finallly press ctrl + F11
Right click on your project, select New -> Source Folder
Enter src as Folder name, then click finish.
Eclipse will then recognize the src folder as containing Java code, and you should be able to set up a run configuration
I had the same issue. I had accidentally deleted the .classpath and .project file in my workspace. Luckily it was in Recycle bin, as soon as it was restored, there were no issues.
Just change "String[] args" to "String args[]".

Matlab doesn't see .jar file

Ok, I'm stumped here. I'm using Matlab version 2013b with a Java RTE of 1.7.0_11 and I'm trying to run a simple piece of code to see if Matlab is able to read the .jar file and nothing seems to be working.
Here is the Java code, which is compiled to a .jar named JavaOCT.jar, which is placed in the Matlab working directory:
package VTK;
public class vtkVolumeView{
public int Test(){
return 10;
}
}
That is it, no other dependencies, nothing fancy. In Matlab, I try:
javaaddpath('\JavaOCT.jar'); %<-Directory and name are 100% correct
import VTK.*; %<-Package name from above
methodsview VTK.vtkVolumeView; %<-Can't find the class, argh!
Matlab kicks back that it can't find the class.
Things I've done to try and solve the problem:
Reverted to the exact same JDK as the Matlab RTE
Tried an older 1.6 JDK
Done lots of stack overflow research to try and solve it 1 2 3 4
Tried used javaclasspath and pointing to the compiled class instead
Read the Matlab documentation 5
Using clear -java after the javaaddpath
Any help would be appreciated, it is driving me nuts!
Update: Daniel R suggested just javaaddpath('JavaOCT.jar') which doesn't work either.
Final update: It finally works! I wasn't building the .jar properly. In IntelliJ, click on the project and hit F4. This brings up the Project Structure, then go to Artifacts and click the green + button and add DirectoryContent and then point to the out\production. Once this is done, as mentioned by others, it should show up in Matlab as an expandable .jar.
I don't know which operating system you are using, but the ./ seems invalid.
Try javaaddpath('JavaOCT.jar'); or javaaddpath(fullfile(pwd,'JavaOCT.jar'));.
What does exist(fullfile(pwd,'JavaOCT.jar')) return?
Some things to try:
Add the class file. When using a package, you need to add the class file in at the host of the package. For example, if your code is here:
\\full\path\to\code\VTK\vtkVolumeView.class
Then use:
javaaddpath('\\full\path\to\code')
I'm still suspicious of your *.jar path. You should usually use absolute paths when adding jar files. Try adding the results of which('JavaOCT.jar')
How did you make your jar file? Does it contain the appropriate directory structure implied by your package declaration?

Error: Selection does not contain a main type

I am trying to run some java files in a new project. So I make the project, put the files in it and I try to run the main file so my game starts.
I get an error that says selection does not contain a main type.
I have tried several ways to run it:
Some say to launch eclipse again, tried this a dozen times.
Somewhere else someone pointed to open a new project and make a build path to the old project.
Didn't work either.
I am pretty sure it must work because I ran it a few hours ago at school. How do I get this working? Thank you in advance!
Right click on the folder where you put your main class then click on Build Path --> Use as Source Folder.
Finally run your main file as java application. Hope this problem will be solved.
If the option 'Use as Source Folder' is not visible then inside the 'Build Path' select the option 'Remove from Build Path'. This will allow 'Use as Source Folder' option to appear in the 'Build Path'.
The other answers are all valid, however, if you are still having a problem you might not have your class inside the src folder in which case Eclipse may not see it as part of the project. This would also invoke the same error message you have seen.
I hope you are trying to run the main class in this way, see screenshot:
If not, then try this way. If yes, then please make sure that your class you are trying to run has a main method, that is, the same method definition as below:
public static void main(String[] args) {
// some code here
}
I hope this will help you.
The entry point for Java programs is the method:
public static void main(String[] args) {
//Code
}
If you do not have this, your program will not run.
I resolved this by adding a new source folder and putting my java file inside that folder. "source folder" is not just any folder i believe. its some special folder type for java/eclipse and can be added in eclipse by right-click on project -> properties -> Java buld path -> Source and add a folder
Few things to check out:
Do you have a main package? do all of your classes are under this package?
Do you use a main class with public static void main(String[] args)?
Do you declare: package ; in your main class?
You can always clean the project before running it. In Eclipse - Just go to Project -> clean then run the app again.
I ran into the same problem. I fixed by right click on the package -> properties -> Java Build Path -> Add folder (select the folder your code reside in).
I am running eclipse from Ubuntu. Had this same problem and was able run the program through terminal. So I just moved the existing public static void main(String[] args) { just below the class declaration (it got automatically formatted by eclipse) and the next launch was successful. Then moved the main method back to where it was before and it worked fine this time.
I had this problem in two projects. Maven and command line worked as expected for both. The problems were Eclipse specific. Two different solutions:
Project 1): Move the main method declaration to the top within the class, above all other declarations like fields and constructors. Crazy, but it worked.
Project 2): The solution for Project 1) did not remedy the problem. However, removing lombok imports and explicitly writing a getter method solved the problem
Conclusion:
Eclipse and/or the lombok plugin have/has a bug.
Looks too late to answer but might help someone,
Having same problem i solved it by following steps:::::
Select Main class in eclipse then click on Window in menu bar,
Window-->Show view-->Select Outline
Right click on main(String[]):void then Run As --> java Application
By doing this you can run the main method directly. This worked for me
Right Click > Run AS > Run Configurations
In this screen if your "Main class" Text field is empty, then add the class name by clicking "Search" button on the right side of the text field and choose the class file. And then click "Run" button on the bottom of the configuration screen. That's it
You must place all your files (file.java) under the root folder SRC.
Make sure the main in public static void main(String[] args) is lower case. For me it didn't work when I had it with capital letter.
Put your Main Java class file in src/main/java folder and check if there is not any error in 'Java Build Path' by following right click on project and select Java Build Path->Source.
If you are working with a Maven project you have to understand the fact that directory layout is bit different. In this the package name must be src/main/java.
For this update your source folder by right click on project root folder -> properties -> java build path -> source tab. Here remove all other source folders as they might have been added in wrong manner. Now, select project /src/main/java as the source folder. Add and apply the changes. Now refresh your workspace using F5.
This should fix the issue of not recognizing a main type.
I ran into the same issue and found that there was an extra pair of braces (curly brackets) enclosing public static void main(String args) { ... }. This method should really be at the top scope in the class and should not be enclosed around braces. It seems that it is possible to end up with braces around this method when working in Eclipse. This could be just one way you can see this issue when working with Eclipse. Happy coding!
I had this happen repeatedly after adding images to a project in Eclipse and making them part of the build path. The solution was to right-click on the class containing the main method, and then choose Run As -> Java Application. It seems that when you add a file to the build path, Eclipse automatically assumes that file is where the main method is. By going through the Run As menu instead of just clicking the green Run As button, it allows you to specify the correct entry-point.
When you save your file, make sure it has the extension .java. If it does not, Eclipse won't know to read it as a java file.
I had this issue because the tutorial code I was trying to run wasn't in the correct package even though I had typed in the package name at the top of each class.
I right-clicked each class, Refactor and Move To and accepted the package name suggestion.
Then as usual, Run As... Java Application.
And it worked :)
You must check this as well, Go to Java build path -> config build path, check that JRE System Library [Java SE -version] is check marked, try running the code again. This fixed my issue.
In my case I was using ant to perform the build, and clearly had the class setup in the build.xml file:
<target name="jar">
<jar destfile="ec-stats.jar" includes="bin/**,src/**">
<manifest>
<attribute name="Main-Class" value="my.package.MyStatistics" />
</manifest>
</jar>
</target>
But it still kept giving me the same error, until I right-clicked on the folder in Eclipse and chose Build Path -> Use As Source Folder.
See also Error: Selection does not contain a main type
Solved the issue as following:
Going in Properties of the Project
1.1) Inside "Java Compiler": chose the: "Compiler compliance level:" to a specific version of JDK (in my case I choosed 15)
1.2) Inside "Java Build Path", in the tab "Libraries", at the Modulepath: change the "JRE System Library" to the same of the version you choosed at step 1.1 above (in my case I picked JDK 15)
Run the java file that contains a main method
This can be resolved in two steps if you are using Eclipse IDE.
👉 Step I: Right click on the project and click Maven > Choose Up-date Project
👉 Step II: Select the project the click Ok button
Hope this helps.✔
This error usually occurs because jdk is not selected for the project. I had the same problem and this worked for me.
Make sure that your class file is inside src folder then follow below steps
Step 1: Right click on project folder
Step 2: Build Path --> Configure Build Path
Step 3: Select "Java Build Path" from the left corner of the window
Step 4: Under "Order and Export" Tab
Step 5: Click on any of the jdk available check box to select it.
Step 6: Click on Apply and Close.

JPL/SWI Prolog configuration failure

I'm configuring the JPL right now, and wanna work with swi-prolog using java.
I downloaded the newest stable version of SWI-Prolog, which is 6.2.0, and installed in D:\swipl
First, I added the following path to the PATH virable: D:\swipl\bin, which should include all dll files needed.
Then, I added the following path to the CLASSPATH virable: D:swipl\lib\jpl.jar, which should be the jar file needed.
When I tried to run the versions example provided, I got the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: jpl.fli.Prolog.thread_self()I
at jpl.fli.Prolog.thread_self(Native Method)
at jpl.Query.open(Query.java:286)
at jpl.Util.textToTerm(Util.java:162)
at jpl.Query.Query1(Query.java:183)
at jpl.Query.<init>(Query.java:176)
at Versions.main(Versions.java:11)
After searching online, I found that many people just get java.lang.UnsatisfiedLinkError: no jpl in java.library.path which is because of the setting for the PATH variable, rather than the error I get here: java.lang.UnsatisfiedLinkError: jpl.fli.Prolog.thread_self()I (and yes, there is a "I" at the end of the line).
Has anyone gotten this error before? I've tried several previous version of SWI-Prolog, but also got other kinds of errors. I'm using Eclipse IDE for Java development -- have I missed anything?
I've sent the problem to the official mailing list (swi-prolog#lists.iai.uni-bonn.de) provided by swi-prolog.org, and luckily someone helped me to prove that there are some problems in the version 6.2.0. We then both tried the version 6.0.2, and it works perfectly. He mentioned that (and I noticed that) there is no swipl.dll in the bin folder of the version 6.2.0, which MAY causes the issue.
I've already reported the issue to the staff via Email, and at least for now, I suggest that people who want to configure JPL should download the version 6.0.2. Three things to remember:
add a new variable SWI_HOME_DIR under system variables in environment variables, and set the path to the place where you installed the SWI-Prolog (Mine is D:\swipl);
Add the path %SWI_HOME_DIR%\bin to your PATH variable, rather than use something like "D:\swipl\bin". (Otherwise [FATAL ERROR: Could not find system resources] will occur)
Add the path %SWI_HOME_DIR%\lib\jpl.jar to your PATH variable, rather than use something like "D:\swipl\lib\jpl.jar". (Otherwise [FATAL ERROR: Could not find system resources] will occur)
If you are using Eclipse for Java development, it seems that you DO NOT need to configure in your IDE. As long as you follow the 3 steps above and add the correct jar file as an external library, it should be fine.
I'm not sure whether the temporary solution works for everyone, but definitely, everyone who has the issue should try this method first. As long as the issue in the version 6.2.0 has been figured out, I'll add some comments here.
BTW, as far as I know, until now, people who have the issue are using 32-bit Windows.
Try adding your path to java.library.path via Run > Run Configuration > [project name] and add the following under "VM Arguments" tab.
-Djava.library.path="D:\swipl\bin;."
Furthermore, under the "Environment" tab, add the following:
VARIABLE: PATH
VALUE: D:\swipl\bin;${env_var:PATH}
After that, go to Project > Properties > Java Build Path, select "Libraries" tab.
Click "Add External JARS.." and find your jpl.jar.
Great Great Great, second answer is the solution
create SWI_HOME_DIR variable to set the swi prolog instalation directory
SWI_HOME_DIR ------- C:\Program Files\swipl
set PATH to point to the library and bin like this
PATH ------ %SWI_HOME_DIR%\bin;%SWI_HOME_DIR%\lib\jpl.jar
This fix my problem "Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpl in java.library.path windows" it is a little bit rare but it works find.
I had the same problem. In addition to set the PATH, you need to verify if the installed SWI program has the same architecture (32 or 64) of your JVM.

Categories

Resources