The declared package "" does not match the expected package - java

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.

Related

Main.java is not on the classpath of project NewProject_880753af, only syntax errors are reported - Java(32)

Yesterday I opened VS Code after a month or so, and I noticed, that all my projects' Java files can't run. I see a light blue curved underline under the first letter in all my Java files, which says:
Main.java is not on the classpath of project NewProject_880753af, only syntax errors are reported - Java(32)
I have my Java file inside "src" directory inside my project directory "NewProject".
When I try to run my file, it then prompts me to add the parent folder to some Java source path:
The file Main.java isn't on the classpath, the runtime may throw class not found error. Do you want to add the parent folder "NewProject/src" to Java source path?
Then it gives me two options - "Add to Source Path" or "Skip". If I press "Skip", it shows an error again:
Cannot resolve the modulepaths/classpaths automatically, please specify the value in the launch.json.
If I choose "Add to Source Path", then it creates a folder called ".vscode" with a file "settings.json" in which there's the following code:
{
"java.project.sourcePaths": [
"src"
]
}
And after that it works... but... I just don't understand - why did Visual Studio Code fail to run my file even if it was working everytime I ran it in the past and without any prompts to add my parent folder to source path or anything?? And I didn't need any "launch.json" files!
How can I fix this problem without creating any new folders inside my project directory?

VS Code: The declared package "controller" does not match the expected package ""

So, I have checked some other threads that are about this topic, such as the following:
Eclipse says package declaration does not match expected package ""
The declared package test does not match the expected package
libgdx: The declared package does not match the expected package ""
But they all deal with Eclipse (or Eclipse + Maven), and I am just using VS Code without all the fancy capabilities of an IDE or a package manager.
As the title suggests, I am getting the message:
[Java] The declared package "controller" does not match the expected package ""
I get this message for every single file that I attempt to give a package using the syntax:
package myPackageName;
even if the file in question is defiantly in the right folder. This problem only occurs as a visual, i.e. everything compiles fine using javac. I have been dealing with it ever since I started using VS Code for my Java Projects.
How do I make the red squigglies and the red lettering in the file explore tab go away in the case of perceived package errors?? It drives me nutty, I can't quickly find the files that actually have errors in my project because everything is marked red except the Driver.
Here is a picture: picture
[edit] Got rid of the unrelated compile time errors So you can see it compiles.
All classes in the package should be open on VSCode (on different tabs).
Same thing happens when you want to use other public classes in same directory: it should be visible but when you run the program, you will get a compile-time error. Then again the solution is to have all classes open on different tabs.
Hope it helps
A bit late to it, but here is my finding:
Your parent folder is proto9, so in all your child folders, i.e. controller, model and view. You should write package proto9.controller, package proto9.model etc. You can compile the .java file just fine in the correct directory, but not 100% sure why we cannot run it while in the directory of the file is at, java.lang.NoClassDefFoundError is returned when attempted so.
To successfully run it, in the terminal (assuming your files are compiled):
# cd one level out of your parent directory
PS C:\Users\Issac\Dropbox\Apps\Celerious Text\proto9> cd ..
PS C:\Users\Issac\Dropbox\Apps\Celerious Text> java proto9.controller.MainController
...
PS C:\Users\Issac\Dropbox\Apps\Celerious Text> java proto9.view.MainView
etc.
Hope this works for you as well!!

IntelliJ: How to resolve java.lang.ClassNotFoundException: main.java.org.***.Main

I'm using IntelliJ and the path to the directory that includes my source code is:
MyProjectFolder/src/main/java/org/***/
This directory includes a java file named Main.java, and in Run/Debug Configurations I have the path "/home/matan/Projects/java_phone_book/src/main" in Working directory field.
Still, when I run the program by IntelliJ, I get the error:
Exception in thread "main" java.lang.ClassNotFoundException:
main.java.org.***.Main
What can resolve this problem?
Your class' fully-qualified name is not main.java.org.***.Main. That is why the class main.java.org.***.Main can not be found.
If you are following a currently popular project layout, then 'main' and java are not part of the package name for your class. Try org.***.Main instead. (although asterisks are not a valid package name, so what you posted can't actually be it)
Otherwise, you'll need to go back to review your CLASSPATH, your package declarations, and your project layout and ensure they all agree with each other.
Go to the menu File > Project Structure. Click the "Modules" section, then click your module name, then click the "Sources" tab. One of the folders shown should be marked as a "Source Folder" already. I will call the name of this folder "S". Make sure Main.java is somewhere in one of the sub-folders of S.
Now, find the full directory path of Main.java. This should be something like S/<some_path>/Main.java. If the beginning of <some_path> is "java" then you should rename that directory to something else. Finally, in Main.java, change the first line of the code so that it says package <some_path>; Replace <some_path> with the one you found in the last step. Replace the slashes with dots.

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.

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.

Categories

Resources