# [java] package keyword behaviour changed?

This topic is 4627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I remember a few years ago writing some java code and using the 'package' keyword. Suppose I had a file called NonPlayerCharacter.java that looked something like this:
[source="java"]package game.server;

public class NonPlayerCharacter implements CellOccupant
{
// ...
}


If I compiled it, as far as I can remember, the compiler would generate game/server/NonPlayerCharacter.class, creating the directories as needed. Now with JDK 5.0 it just puts the class files in the same directory as the java files. Am I right or is it just my imagination?

##### Share on other sites
The compiler creates a class game.server.NonPlayerCharacter. The source should be
game/server/NonPlayerCharacter.java
relative to the source directory tree root. If nothing else is given, the compiler will generate
game/server/NonPlayerCharacter.class
relative to the same directory tree root. If the class directory root should be elsewhere, use the
javac -d where/to/put/classes/to
switch. The man page says
Quote:
 man pageSets the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed.

This works fine for me since 2001. Don't know how the behaviour was before.

##### Share on other sites
Quote:
 Original post by SteveTaylorAm I right or is it just my imagination?

It's not your imagination. If you use the -d option (for directory) when you use javac (compiler) it will create your directory structure for you.

So if you type:

$mkdir ../bin$javac -d ../bin *.java

It will create all your package directories in the ../bin directory.

Good luck!

##### Share on other sites
Ah yes, thanks for that. I just use -d . and it creates the whole stucture in the current dirctory. Come to think of it I think that's what I used all that time ago. So nothing has changed.

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
17
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632591
• Total Posts
3007250

×