4

I'm working on some Java code in eclipse. Code is contained in a single class called Adder, which in Eclipse is in the package org.processing. The first thing in the class file is the line

package org.processing

Q1) What, exactly is this line doing? Why is there, what's it's role.

The code runs fine in eclipse, however, when I move into the workspace if I go to the src/org/processing/ folder in src, compile with javac Adder.class when I try and run using java Adder I get the following error

java.lang.NoClassDefFoundError: Adder (wrong name: org/processing/Adder)

On the other hand, if I compile from src using

javac org/processing/Adder.java

and I can run it from src using java org.processing.Adder but STILL not from within the processing directory.

Q2) Does this mean that compilation is always relative to directory structure?

Finally, if I remove the package org.processing line from the start are the .class file I can compile and run from within the .class file's directory.

Q3) Why is all this the way it is? I can fully understand enforcing a directory structure for code development, but once you're in bytecode this seems a bit over the top, because now I can (apparently) only run the bytecode from one director (src) using java org.processing.Adder. Now, I'm sure I'm missing the point here, so if someone could point out what it is, that would be great.

Alex
  • 2,000
  • 4
  • 23
  • 41
  • Just wow. Stackoverflow, you never cease to amaze me. Thanks all. – Alex Sep 02 '12 at 03:58
  • 2
    The argument in `java Adder` is not a filename at all. It's a class name, fully qualified with the package name. You’re telling Java to load class `Adder` and run its `main` method. But there really is no class with that name; the class you compiled is `org.processing.Adder`. – Jason Orendorff Sep 02 '12 at 03:59

4 Answers4

2

The compiler has to be able to find related source code files when compiling. This is why the package and directory structure must agree for source code. Similarly, the JVM must be able to find referenced .class files. So the same directory structure is required at runtime. It's no more complex than that.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

Q1) The issue here is that once you got into the folders that represent your package hierarchy, you set that as the working directory. It's gonna look inside of org/processing/Adder for the path org/processing/Adder (essentially looking from the root for org/processing/Adder/org/processing/Adder). You need to call it from the root with the full path. The purpose of packages is A: to organize related classes into groups. And B: Along with A, classes in package Foo.bar can't view private classes in other packages, as they are like internal classes for that package, only the package they're in can use them

Q2) Yes

Q3) The paths are used as a basic structure for the JVM to know where exactly the class files (each containing their bytecode) are. If you change where you call it from, your basically trying to change the location for the JVM to look for the class files, but their true location hasn't changed.

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
1

For Q1: The package declaration allows you to guarantee that your class will never be mistaken for another class with the same name. This is why most programmers put their company's name in the package; it's unlikely that there will be a conflict.

For Q2: There is a one-to-one correspondence between the package structure and the directory structure. The short of it is that directories and packages must be the same, excepting the package is usually rooted under a folder called src.

For Q3: Once it's compiled, the class files will probably be in the appropriate folders in a jar file. Your ant or maven tasks will build the jar file so you won't really have to bother with it beyond getting the ant task set up the first time.

Tony Ennis
  • 12,000
  • 7
  • 52
  • 73
1

The short answer - Packages help keep your project structure well-organized, allow you to reuse names (try having two classes named Account), and are a general convention for very large projects. They're nothing more than folder structures, but why they're used can burn beginners pretty badly. Funnily enough, with a project less than 5 classes, you probably won't need it.


What, exactly is this line doing? Why is there, what's it's role.

The line

package org.processing

is telling Java that this class file lives in a folder called /org/processing. This allows you to have a class which is fully defined as org.processing.Processor here, and in another folder - let's say /org/account/processing, you can have a class that's fully defined as org.account.processing.Processor. Yes, both use the same name, but they won't collide - they're in different packages. If you do decide to use them in the same class, you would have to be explicit about which one you want to use, either through the use of either import statements or the fully qualified object name.

Does this mean that compilation is always relative to directory structure?

Yes. Java and most other languages have a concept known as a classpath. Anything on this classpath can be compiled and run, and by default, the current directory you're in is on the classpath for compilation and execution. To place other files on the classpath, you would have to use another command-line invocation to your compilation:

javac -sourcepath /path/to/source MainClass.java

...and this would compile everything in your source path to your current directory, neatly organized in the folder structure specified by your package statements.

To run them, as you've already established, you would need to include the compiled source in your classpath, and then execute via the fully qualified object name:

java -cp /path/to/source org.main.MainClass

Why is all this the way it is?

Like I said before, this is mostly useful for very large projects, or projects that involve a lot of other classes and demand structure/organization, such as Android. It does a few things:

  • It keeps source organized in an easy-to-locate structure. You don't have objects scattered all over the place.
  • It keeps the scope of your objects clear. If I had a package named org.music.db, then it's pretty clear that I'm messing with objects that deal with the database and persistence. If I had a package named org.music.gui, then it's clear that this package deals with the presentation side. This can help when you want to create a new feature, or update/refactor an existing one; you can remember what it does, but you can't recall its name exactly.
  • It allows you to have objects with the same name. There is more than one type of Map out there, and if you're using projects that pull that in, you'd want to be able to specify which Map you get - again, accomplished through either imports or the fully qualified object name.
Makoto
  • 104,088
  • 27
  • 192
  • 230