By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Error running program from a remote folder

P: n/a
I haven't programmed in Java for awhile and forgot how to get this to work.

I have a program ListFiles developed in c:\java which lists every .JPG and
..GIF file in a folder. It works fine running from that folder. It takes 1
argument, the pathname, so a command could be "java ListFiles ."

But if I try to run it from another folder, using

"java c:\java\ListFiles ."

then I get an error

"Exception in thread 'main' java.lang.NoClassDefFoundError: c:\java
\ListFiles/class"

How can I run this from a folder other than the folder with the .java and
..class files ?
BTW, Here's my code -

===========
import java.io.*;
public class ListFiles {
public static void main(String arg[]) {
/*
** java ListFiles . to list images from the current directory
*/
ListFiles d = new ListFiles();
if (arg.length > 0){
System.out.println("Digital Photo List from " + arg[0]);
d.recurseInDirFrom(arg[0]);
}
}

public void recurseInDirFrom(String dirItem) {
File file;
String list[];
file = new File(dirItem);
if (file.isDirectory()) {
list = file.list();
for (int i = 0; i < list.length; i++) {
list[i] = list[i].toLowerCase();
if (!list[i].startsWith("tn_") && (list[i].endsWith(".jpg") || list
[i].endsWith(".gif"))) {
System.out.println("\"" + list[i] + "\",\t\t\t\" \",");
}
}
}
}

}
===========
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Peter Bassett wrote:
I haven't programmed in Java for awhile and forgot how to get this to work.

I have a program ListFiles developed in c:\java which lists every .JPG and
.GIF file in a folder. It works fine running from that folder. It takes 1
argument, the pathname, so a command could be "java ListFiles ."

But if I try to run it from another folder, using

"java c:\java\ListFiles ."

then I get an error

"Exception in thread 'main' java.lang.NoClassDefFoundError: c:\java
\ListFiles/class"

How can I run this from a folder other than the folder with the .java and
.class files ?


You apparently aren't familiar with the concept of the classpath. The
classpath tells the JVM where to look for classes. The default class
loader used by the JVM interprets the classpath as a sequence of
directories and/or jar files.

In most environments, you can specify the classpath using the
environment variable CLASSPATH or via the -classpath option to the java
process. In DOS, using what I can glean above, this looks like:

set CLASSPATH=C:\java
java ListFiles .

or

java -classpath C:\java ListFiles .

An alternate method is to create an executable jar file. This is really
just a jar file that contains a META-INF/MANIFEST.MF file. The manifest
file must have an entry for the Main-Class. The value of this entry is
the fully qualified class name of the class to execute. Then you could run

java -jar C:\java\ListFiles.jar .

without having to worry about the classpath.

HTH,
Ray
Jul 17 '05 #2

P: n/a
Thanks! I had CLASSPATCH as simply . but now realize that I had to add
this pathname also. Works like a dream!

Pete

Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in
news:e%******************@twister.nyroc.rr.com:
Peter Bassett wrote:
I haven't programmed in Java for awhile and forgot how to get this to
work.

I have a program ListFiles developed in c:\java which lists every
.JPG and .GIF file in a folder. It works fine running from that
folder. It takes 1 argument, the pathname, so a command could be
"java ListFiles ."

But if I try to run it from another folder, using

"java c:\java\ListFiles ."

then I get an error

"Exception in thread 'main' java.lang.NoClassDefFoundError: c:\java
\ListFiles/class"

How can I run this from a folder other than the folder with the .java
and .class files ?


You apparently aren't familiar with the concept of the classpath. The
classpath tells the JVM where to look for classes. The default class
loader used by the JVM interprets the classpath as a sequence of
directories and/or jar files.

In most environments, you can specify the classpath using the
environment variable CLASSPATH or via the -classpath option to the
java process. In DOS, using what I can glean above, this looks like:

set CLASSPATH=C:\java
java ListFiles .

or

java -classpath C:\java ListFiles .

An alternate method is to create an executable jar file. This is
really just a jar file that contains a META-INF/MANIFEST.MF file. The
manifest file must have an entry for the Main-Class. The value of
this entry is the fully qualified class name of the class to execute.
Then you could run

java -jar C:\java\ListFiles.jar .

without having to worry about the classpath.

HTH,
Ray


Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.