-1

I am just starting to learn Java. I have made a project of Java using Visual Studio Code. The file path looks like this project/src/own/test.java.

I have written a simple program:

package own;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;

public class test {
    public static void main (String[] args){
        int randomNumber = new Random().nextInt(7);
        System.out.println("Enter a number");
        Scanner keyboard = new Scanner(System.in);
        int inputNumber = keyboard.nextInt();
        if (randomNumber == inputNumber){
            System.out.println("You won!");
        }else{
            System.out.println("You loose!");
        }
        keyboard.close();
    }
}

Every time I run this is the vscode terminal, it says:

Error: Could not find or load main class test

But it runs fine if package own; line is not there. Vscode automatically included this line. Can anyone tell me why that is so? What is the use of package own.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
varun
  • 13
  • 5
  • Java conceptualizes directories as packages. Directories are the operating system representation of the conceptualization that is a package. Java has a concept of a classpath which is an aggregator of package and dependency locations ( source and target info). So, while you created the folder, you likely didn’t instruct the compiler of its existence, which is done via the classpath file. Why it runs when you remove the package declaration is a bit curious, the compiler maybe recognizes all subdirectories of the root as flat ( root of package tree ) if no other source information is declared. – Chris Maggiulli Sep 05 '22 at 04:21
  • 1
    How exactly are you rinning the program? Can you add whole command? – Piro Sep 05 '22 at 04:26
  • I am using vscode to run the program. There is a run option in vscode. – varun Sep 05 '22 at 04:27
  • In VS Code at left side icon bar go to debug and create / add Run Configuration and select test.java file. – maddy23285 Sep 05 '22 at 04:28
  • It is running with the package own line if I do run/debug. – varun Sep 05 '22 at 04:39

1 Answers1

0

Classes in Java almost always have to specify their "package...". Almost because there are some exceptions. This reserved word is used for several important things, such as:

  • With package you identify in which zone of the project your class is located, in general, each java project has src/main/java/mypackage

    • src, main, and java are directories, not packages (you can look up the difference between these). So the classes here will not have a package, you can execute a main method but this class will be invisible inside your project
    • If you are at the "mypackage" level, from this path the class is already visible inside your project and the class will carry the "package mypackage";
  • By having your class with "package mypackage;" you can import this class to another class, and use the methods of the first class in the second class

package mypackage;

public class ClassOne {
   public static String goodMorning(){
       System.out.println("good morning");
   }
}

And this class is in another file

package mypackage;

import mypackage.ClassOne;

public class ClassTwo{
    public static void main(String args...){
        ClassOne.goodMorning();
    }
}

If you know PHP it works similar to "require_once" (although they are similar import and require_once do not work the same)

  • I mentioned above about "visibility" and it is that packages are also used to know the scope of an access modifier, in java there are 4 of these: public, private, protected and default (by default it is when you do not put an access modifier )
//public access modifier
public class One{
    public String attribute;

    public void method(){
    }
}
//private access modifier
private class Two{
    private String attribute;

    private void method(){
    }
}

//protected access modifier
protected class Three{
    protected String attribute;

    protected void method(){
    }
}

//default access modifier
class Four{
    String attribute;

    void method(){
    }
}

Find out what each one is for.

  • And another thing it is useful for, is organizing classes by packages, you can organize a collection of classes in a better way, this is important for large projects.

There are still more things but as you say that you are learning I do not want to overload you with information, go easy, greetings.

  • 2
    _"Every java class must always specify the "package ...""_ incorrect, you can have classes in the default package, but that is only really useful for toy programs, and doesn't provide the benefits of packages (namespacing, breaking things up in smaller manageable units, etc.) – Mark Rotteveel Sep 05 '22 at 08:12
  • @MarkRotteveel I know, it was my mistake when writing it, then I realized, if you read a little further down, I mention what you say, that in a directory the package should not be specified. (My fault too for not correcting it) – Marcos Lopez Sep 05 '22 at 08:48