开发者

Making java packages

My Java classes organization has gotten a little messy so I'm going over something I've skipped in my Java learning: the classpath. I can't quiet get beloved classes to compile in the packages I have created for them. Here's my folder hierarchy:

.
com/
    david/
        Greet.java

        greeter/
            SayHello.java
            SayG开发者_如何学Pythonoodbye.java

Where SayHello's constructor just prints "hello" and SayGoodbye's prints "Goodbye" and Greet's main method just creates those two objects. At the top of SayHello is package com.david.greeter; and likewise with SayGoodbye and Greet's is package com.david;

In the greeter folder I was able to compile both java files but if I go to the current directory (the directory that holds com) and do javac -cp "com.david.greeter.*" com/david/Greet.java it says it can't find the classes as well as saying package com.david.greeter doesn't exist. I've also tried setting the $CLASSPATH manually.

I'm at my wit's end here, Stackoverflow (as I normally am when I post here). Do any of you know what I am doing wrong?


The java compiler will traverse the sub-directories of the classpath looking for the packages it needs.

So, your command line should be as follows:

javac -cp "." com/david/Greet.java

When the compiler sees a reference to com.david.greeter.SayHello while compiling Greet.java it will start with the directory in the classpath and traverse the hierarchy looking for the package it needs.


First, as documented in Setting the Classpath, the way you're currently setting your class path is wrong. Class path entries should be filename or directory. So using com.david.greeter.* doesn't make any sense. Second, the current directory is in the class path by default:

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

So if you execute javac (here is the man page by the way) from the folder containing com, you don't have to tweak anything, just type:

javac com/david/Greet.java

And javac will go through the directory tree to find references (e.g. SayHello if you're using it from Greet) and compile them too.

And by the way, if you have to set the class path, don't use the $CLASSPATH environment variable, this is just a bad practice in most case, prefer the -cp option.


If you are in the folder containing com, then try this:

javac -cp . com\david\Greet.java


This is incorrect (as the compiler has already told you):

javac -cp "com.david.greeter.*

Open a command shell and navigate to the directory that contains the "com" directory.

I think you really want this to compile SayHello.java and SayGoodbye.java:

javac -cp . com/david/greeter/*.java

This to compile Greet.java:

javac -cp . com/david/*.java

And this to run:

java -cp . com.david.Greet


The "com" directory should not be current, it should be a child directory to current. You need step one level upper and launch again. No extra care about classpath should be needed at this point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜