0

I'm just getting started with Java. This is my program:

package javaapplication1;

public class JavaApplication1 
{
    public static void main(String[] args)
    {
        System.out.println("Hello ");
    }
}

If I remove the first line package javaapplication1, the code won't run. I did the same thing in class but it was working. Can someone explain why does this happen?

Nick Hill
  • 71
  • 8
  • have you learn java?, how the compiler will found the class full path, if you don't provide the package name, it completes the class full name. – ArifMustafa Jan 02 '18 at 16:15
  • On Eclipse or Comand Line? – Marcos Vasconcelos Jan 02 '18 at 16:15
  • @MarcosVasconcelos Netbeans – Nick Hill Jan 02 '18 at 16:15
  • Possible duplicate of [What does "Could not find or load main class" mean?](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – pzaenger Jan 02 '18 at 16:16
  • @pzaenger I did look at that question but that guy didn't remove the first line. – Nick Hill Jan 02 '18 at 16:16
  • 2
    Possible duplicate of [What is the purpose of defining a package in a Java file?](https://stackoverflow.com/questions/1088509/what-is-the-purpose-of-defining-a-package-in-a-java-file) – user3437460 Jan 02 '18 at 16:18
  • 1
    Your file is inside a folder named `javaapplication1` inside the source root for your project. By deleting the package declaration the class becomes part of the default package, and however you were running it before no longer works. (it refers to it as `javaapplication1.JavaApplication1`) – Neil Locketz Jan 02 '18 at 16:19
  • @NeilLocketz I did the same thing in class, it was working. – Nick Hill Jan 02 '18 at 16:21
  • I can see that from the question. Obviously you are doing *something* different, otherwise it would work the same as it did in class. What would help us diagnose the problem is if you give us some kind of error output instead of saying "the code won't run". Does Netbeans spit something out when you try and run it? Post that. We aren't psychic and can only help based on the info you provide. – Neil Locketz Jan 02 '18 at 16:23
  • @NeilLocketz by class I mean my university class. Netbeans doesn't show anything just the error. – Nick Hill Jan 02 '18 at 16:26
  • What is the error? – Neil Locketz Jan 02 '18 at 16:28
  • @NeilLocketz Error: Could not find or load main class javaapplication1.JavaApplication1 C:\Users\Nick\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – Nick Hill Jan 02 '18 at 16:30
  • Your problem is that its looking for a class by the name of `javaapplication1.JavaApplication1`. When you remove the `package javaapplication1` from the file you change the name of the class from `javaapplication1.JavaApplication1` to just `JavaApplication1` and then netbeans can't find it to run it. To fix this you have to fix the project "main class" so that it says just `JavaApplication1`, and you'll need to move the source file up one directory. – Neil Locketz Jan 02 '18 at 16:36

2 Answers2

2

If you are working with terminal along with package statement, save your code as JavaApplication1.java and then compile your current code with below syntax.

javac -d . JavaApplication1.java

( -d . indicates create directory in current location because we are using package statement). then for execution of your code you need to change directory with

cd javaapplication1

then execute your code with

java JavaApplication1

It will execute fine.

But if you are working without package statement seen will differ, you need to compile code normally with

javac JavaApplication1.java

then execute code with

java JavaApplication1

You will not get an error.

Note: But If you are using any IDE nothing to worry about. IDE will take care of package statement.

error: Could not find or load main class: this error appears at runtime if JVM is unable to find main class.

You need to change the directory as I mentioned above, then it might work fine.

1

This has to do with the way your IDE sets up your project.

When you start learning a new language, it's always a good idea to skip the IDE until it actually becomes useful. I suggest you copy your program to a basic text editor, remove the package line, save it as JavaApplication1.java and manually compile and run with javac JavaApplication1.java and java JavaApplication1.