I am very new to java and when I create a project, it makes the src and the JRE System Library. What it doesn't create is a default package. When i make a class in the src it names it after the project. it requires me to put package (name of project). I don't want to. How do i make the default package.

- 228
- 1
- 5
- 11
-
What is the reason for having the class inside the default package? see also http://stackoverflow.com/questions/7849421/is-use-of-javas-default-package-bad-style – mschenk74 Jul 04 '14 at 21:01
2 Answers
New -> class
Delete the package field and name the class whatever you want. Make sure it's in the right project.
If you have a class that is already in the wrong package, simply delete the package name from the top of the file and drag the file in the file viewer to the src folder itself.

- 3,932
- 13
- 27
This situation is EXTREMELY FRUSTRATING to beginners, especially when they are new to both Eclipse AND Java!
Specifically, Eclipse won't compile without a package (thus it provides a "(default package)" when you make a new class without a package), and Java won't run without a "main" class (which is whichever class has a method named "main" in it; usually, for simple beginner programs, declared as public static void main(String[] args)
).
This comes to a head when, without knowing better (as there is NO tutorial within the Eclipse IDE), you (a beginner):
- create a new project, then
- create a new .java file (which Java will interpret as a
class
, but Eclipse won't put into a package), and then - try to run the perfectly valid Java program with a "main" method and everything in the Eclipse environment.
No dice. Will not work.
At this point, especially if you have already put hours into making your program, you need a way to FIX THIS PROJECT so that Eclipse can run it.
Eclipse will NOT allow you to just make a "(default package)", since all packages MUST have names, and NO name can start with an open parenthesis. Thus, you cannot make the (default package). This requires you to FORCE Eclipse to make it for you. How to do this was already explicated in this answer provided by Strikeskids, on 4 July 2014:
Delete the package field and name the class whatever you want. Make sure it's in the right project. If you have a class that is already in the wrong package, simply delete the package name from the top of the file and drag the file in the file viewer to the src folder itself. picture from this answer
Here's the link to that answer: https://stackoverflow.com/a/24580569/20019273
However, I have ALSO needed to RESTART Eclipse after doing those steps in order to effect the necessary changes.
There was no end to my frustration when I did everything correct, but Eclipse still did not generate a "(default package)" for me. Upon closing Eclipse, then reopening it, Lo and Behold the (default package) was there, without any additional changes!
AN ADDITIONAL frustration comes if you are exporting your project to Replit.com. Replit, by default, requires a "Main.java" class file with the "main()" method in it, AND this must be OUTSIDE of ANY packages or folders within your project. From the "main(...)" method in the "Main.java" class, you can THEN call and use your other classes and methods within the other .java files and directories (packages) your project may have.
Thus, to make an Eclipse project that also runs in Replit, make a new Eclipse project (named whatever you want), then make a class called Main (which Eclipse will put into Main.java within the "(default package)" package), then code the "main(...)" method in the Main.java file/class, then have that "main" method call any additional classes/.java files you make. FINALLY, On Replit.com, make a new Repl, then upload the Main.java AND any other .java files that are in your project, MAKING FOLDERS WHEN NECESSARY FOR ANY PACKAGES YOU CREATED FOR YOUR PROJECT. (Most beginner programs do not use additional folders (A.K.A. "packages"), even when making several different classes.) At this point, your project should run in Eclipse AND on Replit.com.
I sure wish there was a tutorial about the MINIMUM requirements for a runnable Eclipse project and a runnable Repl, but, here we are...

- 1
- 1