0

Can anybody here help me with packages. I have understood how packages works.

How to import them and how to make them using the package keyword. How the directories and work. And what is classpath and how to set it.

But the one thing I am not able to get is If I want to create a package and add the location of the base directory in the classpath, what should be in that base directory ??

Is it either the .java files or the .class files or both of them or something called .jar file ?

I don't know what the jar format is either.

10
  • Why don't you use an ide (example: eclipse) if you are still new to java? It will handle all the boring compilation problem for you. It would be easier to start with an ide I would say. But you can think of a package like a folder where classes are stored Commented Apr 16, 2021 at 0:09
  • @DavidAlmeida I am using VSCode and I've tried making different folders in a the base folder and experimented with importing classes from one folder to another. But to do that I've just used the .java files in the other directories and it worked absolutely fine. And now I was thinking If I want to make a package what would I need to do. btw thanks for your reply. Commented Apr 16, 2021 at 0:14
  • Or instead of letting an IDE do it for you, maybe just look at a Java tutorial? There's plenty out there about file structure/imports. Commented Apr 16, 2021 at 0:16
  • 1
    Sorry to everybody If I look like I haven't tried to do the research and put in the effort but I've been stuck over packages for atleast 4 days now it's just killing me I've understood the most but this is the last thing I didn't get. I just want to complete this topic and move on to interfaces, exceptions, i-o streams, multithreading. I mean I am left with a lot of work but have very less time left. Commented Apr 16, 2021 at 0:32
  • 1
    If your package is com.stackoverflow.example then the base folder should contain com. That should contain stackoverflow and that folder should contain example. Even in a jar file (which is a zip file plus some metadata). The base should contain a folder named com. And so on. Finally, com.stackoverflow.example.Main would have Main.class in that third level folder named example. Commented Apr 16, 2021 at 0:42

1 Answer 1

1

A package is a unique namespace for class names. It prevents collisions between programs and between libraries.

For instance, if you make a class called Spreadsheet and several other people also make a class called Spreadsheet, and for some reason all of those classes are available at runtime, only one can be loaded. (Actually it’s more complicated than that, but my statement is true for the most common cases.)

But if your class is named com.kataria.Spreadsheet, it is unlikely to collide with any other classes, since it’s unlikely they that have that package name.

The package name is part of the class name. Java does not have a class named List, but Java does have a class named java.util.List. An import statement is just compiler shorthand; import java.util.List; tells the compiler, “every time I write List, I actually mean java.util.List.”

Since the last part (known as the simple name) of a class name is only a shorthand, the two classes java.util.List and java.awt.List are different classes. There is no conflict between them. (However, if for some reason you wanted to import both, that would be a problem, because the compiler wouldn’t know whether List by itself in the code is supposed to be shorthand for java.util.List or java.awt.List.)

To enforce consistency, Java requires that every (public, top-level) class is in a file which exactly matches its name and package:

  • java.util.List must be defined in a file called java/util/List.java
  • com.kataria.Spreadsheet must be defined in a file called com/kataria/Spreadsheet.java
  • com.example.ai.wopr.Brain must be defined in a file called com/example/ai/wopr/Brain.java
  • etc.

Similarly, .class files must have the same naming and directory structure:

  • The java.util.List class must be compiled to a file called java/util/List.class
  • The com.kataria.Spreadsheet class must be compiled to a file called com/kataria/Spreadsheet.class
  • The com.example.ai.wopr.Brain class must be compiled to a file called com/example/ai/wopr/Brain.class
  • etc.

A search path is a very old operating system concept, far older than Java. All Windows and Unix systems use it for locating programs on the command line, and for locating loadable software libraries.

A search path is an ordered list of directories. When you enter a command, the terminal environment (shell) looks in each directory in the search path for that command.

Similarly, a Java classpath was originally a list of directories. If you enter the command java com.kataria.Spreadsheet, it would look in each directory specified in the classpath for a com/kataria/Spreadsheet.class file.

Not long after that, a Java classpath entry was allowed to be a .zip file or .jar file. Although they are single archive files and not directories, the entries in such an archive has to obey the same rules: to go back to the previous example, the first .jar file in the classpath which has a com/kataria/Spreadsheet.class entry will be the source from which the class is loaded at runtime.

A .jar file is actually a zip file which a different extension and a few special Java-specific entries. It is simply an easier way to deliver a Java program or library than copying around a bunch of files. (Also it is compressed, so it uses less space and can be loaded from disk faster.)

When you want to run a Java program, you normally set the classpath on the command line:

java -cp /home/kataria/my-spreadsheet.jar:/opt/javafx/lib/javafx.base.jar com.kataria.Spreadsheet 

You can also set an environment variable named CLASSPATH; this is based on the fact that the original system search paths have used (and continue to use) environment variables like “PATH” and “LD_LIBRARY_PATH”. I do not recommend setting CLASSPATH, since it will result in your program only running in one particular environment. The program would not run on any other computer unless that user also remembers to set their CLASSPATH variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Good explanation. But in case the OP is still confused, it's worth repeating Elliott Frisch's comment from above: "If your package is com.stackoverflow.example then the base folder should contain com. That should contain stackoverflow and that folder should contain example. Even in a jar file (which is a zip file plus some metadata). The base should contain a folder named com. And so on."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.