more command line woes......

ok - I have 2 copies of a very similar java class called FileStuff - one of them is part of a package called ch10_io the other is not.... I can run the one that is NOT part of the package by typing >java FileStuff. I cannot run the one that is part of the package.

here's the source for both.....

1st the one in the package:

package ch10_io;

import java.io.File;

import java.io.IOException;

importstatic java.lang.System.out;

publicclass FileStuff{

publicstaticvoid main(String args[])throws IOException{

out.print("File system roots: ");

for (File root : File.listRoots()){

out.format("%s ", root);

}

out.println();

for (String fileName : args){

out.format("%n%nnew File(%s)%n", fileName);

File f =new File(fileName);

out.format("toString(): %s%n", f);

out.format("exists(): %b%n", f.exists());

out.format("lastModified(): %tc%n", f.lastModified());

out.format("isFile(): %b%n", f.isFile());

out.format("isDirectory(): %b%n", f.isDirectory());

out.format("isHidden(): %b%n", f.isHidden());

out.format("canRead(): %b%n", f.canRead());

out.format("canWrite(): %b%n", f.canWrite());

out.format("canExecute(): %b%n", f.canExecute());

out.format("isAbsolute(): %b%n", f.isAbsolute());

out.format("length(): %d%n", f.length());

out.format("getName(): %s%n", f.getName());

out.format("getPath(): %s%n", f.getPath());

out.format("getAbsolutePath(): %s%n", f.getAbsolutePath());

out.format("getCanonicalPath(): %s%n", f.getCanonicalPath());

out.format("getParent(): %s%n", f.getParent());

out.format("toURI: %s%n", f.toURI());

}

}

}

now the one that is not part of the package:

import java.io.File;

import java.io.IOException;

importstatic java.lang.System.out;

publicclass FileStuff{

publicstaticvoid main(String args[])throws IOException{

out.print("File system roots: ");

for (File root : File.listRoots()){

out.format("%s ", root);

}

out.println();

for (String fileName : args){

out.format("%n%nnew File(%s)%n", fileName);

File f =new File(fileName);

out.format("toString(): %s%n", f);

out.format("exists(): %b%n", f.exists());

out.format("lastModified(): %tc%n", f.lastModified());

out.format("isFile(): %b%n", f.isFile());

out.format("isDirectory(): %b%n", f.isDirectory());

out.format("isHidden(): %b%n", f.isHidden());

out.format("canRead(): %b%n", f.canRead());

out.format("canWrite(): %b%n", f.canWrite());

out.format("canExecute(): %b%n", f.canExecute());

out.format("isAbsolute(): %b%n", f.isAbsolute());

out.format("length(): %d%n", f.length());

out.format("getName(): %s%n", f.getName());

out.format("getPath(): %s%n", f.getPath());

out.format("getAbsolutePath(): %s%n", f.getAbsolutePath());

out.format("getCanonicalPath(): %s%n", f.getCanonicalPath());

out.format("getParent(): %s%n", f.getParent());

out.format("toURI: %s%n", f.toURI());

}

}

}

I use the same command for each .... do I need to qualify the command for the first version by using the package name somehow?

when I try to run the packaged version I get the error:

Exception in thread "main" java.lang.NoClassDefFoundError: CH10_IO/ch10_io/FileS

tuff

Message was edited by:

thouse

[5860 byte] By [thousea] at [2007-11-27 10:19:03]
# 1

Try compiling and running your packaged class from one directory up ie the parent directory to ch10_io.

floundera at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...
# 2

> Try compiling and running your packaged class from

> one directory up ie the parent directory to ch10_io.

you win the Dukes flounder - when I go up 1 level and I type >java ch10_io.FileStuff it works - if I just type >java FileStuff it doesn't. Any ideas why that is?

thousea at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...
# 3

> do I need to qualify the command for the first version by using the package name

> somehow?

Yes.

When you do what flounder suggests you will have to specify the directory that the source file belongs to when you compile it. (Using some OS specific convention like / or \).

And when you run the program you will have to specify the name of the class. (Using the convention that Java likes: package-dot-name).

[Edit] > if I just type >java FileStuff it doesn't. Any ideas why that is?

As above. But, woe! "zero dukes available"!

pbrockway2a at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...
# 4

Basically package means directory. So when you compile from the ch10_io directory it will look for the class in a sub-directory called ch10_io and of course that doesn't exist.

floundera at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...
# 5

> Basically package means directory. So when you

> compile from the ch10_io directory it will look for

> the class in a sub-directory called ch10_io and of

> course that doesn't exist.

right, but because package basically means directory you'd think that when you're in the directory you'd be able to run the file without qualifying it.... it's a little quirky.

thousea at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...
# 6

A package isn't tied to a file system, it could be in a DB, or other system.

macrules2a at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...
# 7

> right, but because package basically means directory

> you'd think that when you're in the directory you'd

> be able to run the file without qualifying it....

> it's a little quirky.

No, because the argument to "java" is the fully qualified class name. Fully qualified mean package name + class name. Then the JVM is looking for a directory path that matches the package name and contains a file that matches the class name (plus .class).

atmguya at 2007-7-28 16:55:20 > top of Java-index,Java Essentials,New To Java...