I want to run my code without creating a package in java eclipse IDE.
But when I do so it is showing error :
Must declare a named package because this compilation unit is associated to the named module 'Games'
You are using the Java Platform Module System (JPMS) which requires not to use the default/unnamed package.
In the default package delete the file module-info.java to not use JPMS and to be able to have code in the default package.
Just right click on the project folder -> New -> Package. Once the package is made, move all your classes into it.
Double click on the error icon at the line number ->move to default package option.
Make sure the classes are (public or protected) to be used in different packages.
In the file in question, right click to get the context menu. Select "Refactor->Move". The resultant dialog will have the (default package) into which the source file may be placed.
Also, be sure to not have a package definition line at the start of the source file.
Please note that the use of the default package is generally discouraged, but there are time I still use it (primarily for printing copyright/version information).
There are also options for turning on hidden/suppressed parent packages, but I think the Refactor approach is the most clean.
The most viable answer to this problem is to remove the module-info.java file located on the Project Explorer tab of Eclipse. This file is created by default. Also, deleting this will not affect your program. Hope this will help.
Related
I am working on a class within a package within a source folder within a project with Eclipse Version: 2019-06 (4.12.0).
First I did not organize my files well, so I had to move them using Refactor --> Move. Now I created a class within a source folder. On that particular source folder I right clicked and choose Build Path --> Add External Archives, I then selected a UI library and all worked well.
Now I reorganized my classes and moved them to a different project, where I created source folders and packages for the classes. All the classes work well, expect for the one that uses the UI interface. But here is the part that puzzles me: I can not add an external archive anymore. When I right click on the project, I find Build Path --> Configure Build Path as the only option. Now I can select Libraries and choose Add extneral JARs, but this does not make the class work as it did before. In stead, I get errors on all the lines referencing the UI, saying: the type *** is not accessible or *** cannot be resolved to a type and even line where I do not use the interface but simply introduce a string give an error: The type java.lang.String is not accessible.
I am new to Java and completely puzzled. Any help would be much appreciated.
I found the bug. Apparently there was a package-info.java file created in another package which was in the same source project. This, for some reason, blocked the UI-interface. After deleting this file and removing the JRE System Library from Build Path --> Configure Build Path, then adding the exact same JRE System Library fixed the problem.
I cannot run a program in Eclipse Photon (4.8.0). Eclipse keeps telling me that, "Editor does not contain a main type." Even though it clearly does. According to Package Explorer, the java file I'm trying to run is under the src folder, so Eclipse should be able to find it.
So, I think this is because my project doesn't have a default package. Or at least, not one that I can see.
My question is, how can I add a default package to an already existing project in Eclipse?
If you choose to down-vote this, please let me know why. I would like to improve my post if possible.
Here is a screenshot of my Package Explorer.
You cannot use the default package in a project which has a module-info.java.
If you don't need the module system's features, just remove the module-info.java and you can develop as pre java 9.
You can't add a default package but you can add a package by right clicking on the folder and creating a new package and after that adding package "packagename"; on top of the file.
In the Package Explorer panel, right click on your "src" folder, and create a new class. In the window that pops up to ask you for the name of the class (see the picture below), there is a section that has the name of the package. Not providing any name there, would result in the new class to be saved in the "default package".
Note that, default package means that there is no package to hold that class. This is just a way for eclipse to show you that this class is not kept in any specific directory in your computer. It will be stored right inside the "src" folder, next to other packages.
I just started taking this Distributed Systems class, and my teacher says the following on our class website:
Please do NOT use packages! If your configuration of Eclipse uses them
by default, please remove them before writing code!
What the heck is she talking about? I thought eclipse NEEDS to create packages for your project source files to stay organized. How do I remove them?
Packages are not mandatory. If you create a new project in Eclipse, you can directly add your classes to src folder. These files will be under /workspace/project_name/src/ folder. If you add a package, your class files will be placed under /workspace/project_name/src/package_name folder. Your instructor will need to know package names to be able to compile your files and she is probably using a script or something to automatically compile them and that script assumes your files are placed under src folder.
Just remove the first line of your code if exist that says:
package <package-name>;
And if you are creating a new class, then don't give any package name.
I have a problem. I had project in eclipse. I changed my IDE to netbeans.
Now i have problem, because in eclipse i had two source folder:
src/resources/main
src/java
All my jars i have in src/java, so i added src/java to my source in netbeans.
But netbeans cant find class with "main" function in this project when this class is in src/java. Set main class in properties doesnt work.
But when i move it to src/resources/main it works fine.
Is it possible to have main class in src/java? Because i dont want change my structure
There are two folders, and two issues. We will fix them one at a time. Then they will be explained.
make a main directory just under src
move the src/java folder into src/main, the result should be src/main/java
Then to fix your resources folder
move the src/resources/main into src/main renaming it at the same time to src/main/resources.
Now the rationale. This isn't really about Eclipse or Netbeans, it is about Maven. Maven imposes a particular directory naming convention. Maven has put some time and effort into the convention, and it is generally well thought out. More importantly, Maven has been pretty popular, and it's convention is seen as a de-facto standard.
Your Netbeans project isn't properly configured. However, you could solve it one of two ways: you could configure every detail, or you could adapt to the standard expected conventions. Since this isn't just Netbean's conventions, but also Maven's conventions, and most IDEs expect these conventions (or at least accommodate them), it is probably a better idea to follow them than to reconfigure you Netbeans to find sources and resources in nonstandard locations.
You can manually specify the main entry class.
Right click on your Project in the project explorer
Click on properties
Click on Run
Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
Click OK.
Run Project :)
I have some classes in my current project which have the wrong package declaration (they are in the wrong folder for their declared package.)
Unfortunately, fixing the problem by moving the class is not an option. Is there a way I can get eclipse to ignore the error?
You can fiddle with Eclipse's display in the "Errors/Warnings" section of preferences, but Java requires that you have files in the correct folder to match their package, so it won't be able to build
If fixing the problem by moving the class is not an option, is fixing the problem by changing the declared package name an option? If you can't do one of those two things, the code won't compile -- so this wouldn't be an Eclipse problem so much as a Java problem.
Do you have the source folder for Eclipse at the right level? The source folder for Eclipse should be at the level where the package folders start. If there are multiple folders with separate package structures, each one would be it's own source folder.