Yet another compiling packages help request!

Hi folks. If you're here, thank you in advance. I know people ask about this all the time, but I always find the explanations (and the whole topic of compiling packages) confusing.

Perhaps someone could help me out. I have files in the java_home/com/osip/excelLoad directory with "package com.osip.excelLoad;" statement. I also files in the com/osip/utes/fileUtes directory with "package com.osip.utes.fileUtes;"

So when I compile the first package can I import a class from the 2nd pakcage (i.e. I'm at the java home trying to compileg com/osip/excelLoad/*.java ... one of the files in this director has "import com.osip.utes.fileUtes.*;")

I can't get the compiler to recognize this package.

Here's what I'm typing to compile:

[at the java_home directory]javac -classpath com/osip/utes/fileUtes:$CLASSPATHcom/osip/excelLoad/*.java

Am I missing something? Any help would be appreciated as I'm going crazy trying to do this.

Here's what I'm actually seeing;

[cwhalen@bo-web02 j2sdk1.4.2_06]$ javac -classpath com/osip/utes/fileUtes:

javamail-1.3.2/mail.jar:$CLASSPATH com/osip/excelLoad/*.java

com/osip/excelLoad/GetXLSMail.java:8: package com.osip.utes.fileUtes does not exist

import com.osip.utes.fileUtes.*;

^

com/osip/excelLoad/GetXLSMail.java:26: cannot resolve symbol

symbol : class StreamToFile

location: class com.osip.excelLoad.GetXLSMail

StreamToFile stf = new StreamToFile();

^

com/osip/excelLoad/GetXLSMail.java:26: cannot resolve symbol

symbol : class StreamToFile

location: class com.osip.excelLoad.GetXLSMail

StreamToFile stf = new StreamToFile();

^

3 errors

java_home

|

|--com

|

|-osip

|

|-excelLoad

|

|-utes

|

|-fileUtes

[1902 byte] By [javverjaw] at [2007-9-30 22:28:31]
# 1

> So when I compile the first package can I import a

> class from the 2nd pakcage (i.e. I'm at the java home

> trying to compileg com/osip/excelLoad/*.java ... one

> of the files in this director has "import

> com.osip.utes.fileUtes.*;")

>

> I can't get the compiler to recognize this package.

>

> Here's what I'm typing to compile:

>

> [at the java_home directory] javac -classpath

> com/osip/utes/fileUtes:$CLASSPATH

>com/osip/excelLoad/*.java

>

Presumably both packages are under /java_home/com/...

If so the class path is the "/java_home". So the command would look like...

javac -classpath /java_home ....

jschell at 2007-7-7 12:51:50 > top of Java-index,Security,Event Handling...
# 2
wow. You the man. I just figured that when you're in a given directory the classpath implicity included that directory. Not so?
javverjaw at 2007-7-7 12:51:50 > top of Java-index,Security,Event Handling...
# 3

> wow. You the man. I just figured that when you're in

> a given directory the classpath implicity included

> that directory. Not so?

Yes but that is still relative to the package. Thus if you are in /java_home it is going to find your classes in the sub directories below that. But it isn't going to find them in fileUtes because there is no directory ../fileUtes/com/osip/...

jschell at 2007-7-7 12:51:50 > top of Java-index,Security,Event Handling...
# 4

This is a minimal explanation of packages, maybe it'll help

Assume that your programs are part of a package named myapp, which is specified by this first line in each source file:

package myapp;

Also assume that directory (C:\java\work\) is listed in the CLASSPATH list of directories.

Also assume that all your source files reside in this directory structure: C:\java\work\myapp\

Then a statement to compile your source file named aProgram.java is:

C:\java\work\>javac myapp\aProgram.java

And a statement to run the program is:

java myapp.aProgram

(This can be issued from any directory, as Java will search for the program, starting the search from the classpath directories.)

Explanation:

Compiling

A class is in a package if there is a package statement at the top of the class.

The source file needs to be in a subdirectory structure. The subdirectory structure must match the package statement. The top subdirectory must be in the classpath directory.

So, you generate a directory structure C:\java\work\myapp\ which is the [classpath directory + the package subdirectory structure], and place aProgram.java in it.

Then from the classpath directory (C:\java\work\) use the command: javac myapp\aProgram.java

Running

-

Compiling creates a file, aProgram.class in the myapp directory.

(The following is where people tend to get lost.)

The correct name now, as far as java is concerned, is the combination of package name and class name: myapp.aProgram (note I omit the .class) If you don't use this name, java will complain that it can't find the class.

To run a class that's NOT part of a package, you use the command: java SomeFile (assuming that SomeFile.class is in a directory that's listed in the classpath)

To run a class that IS part of a package, you use the command java myapp.aProgram (Note that this is analogous to the command for a class not in a package, you just use the fully qualified name)

ChuckBing at 2007-7-7 12:51:50 > top of Java-index,Security,Event Handling...