File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Basic CLASSPATH problem on Mac OS X Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic CLASSPATH problem on Mac OS X" Watch "Basic CLASSPATH problem on Mac OS X" New topic
Author

Basic CLASSPATH problem on Mac OS X

Alex Hillel
Greenhorn

Joined: Sep 18, 2007
Posts: 3
Hi.

I am another one of those useless folk having a basic CLASSPATH problem.

I am using Mac OS X 10.4.10, and am getting errors when i try to run compiled programs that specify a package.

I set my CLASSPATH variable in ~/.profile with the following line:
export CLASSPATH="$CLASSPATH:.:~/javaprogs"

The path structure to my classfile TestPackages.class is:
~/javaprogs/com/alexhillel/test/TestPackages.class

The code of TestPackages.java (which seemed to compile correctly) is:

package com.alexhillel.test;
public class TestPackages {
private static int i = 3;
public static void main(String[] args) {
System.out.println("i = " + i);
}
}

However, when i run the program from the 'test' directory with the command 'java TestPackages' i get the following output:

Exception in thread "main" java.lang.NoClassDefFoundError: TestPackages (wrong name: com/alexhillel/test/TestPackages)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

Any ideas why? echo $CLASSPATH gives me :.:~/javaprogs (which seems fine).


Thanks,
Alex
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

Note the error message when trying to run: "...TestPackages (wrong name: com/alexhillel/test/TestPackages)..."

So I don't think the problem is with the classpath. Instead, the problem is in the qualified name of the file.

This class is declared as part of the package com.alexhillel.test, so you should run this from javaprogs (the directory that contains com) using the following command...

>java com.alexhillel.test.TestPackages
[ September 18, 2007: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Alex Hillel
Greenhorn

Joined: Sep 18, 2007
Posts: 3
Thank you, that worked fine... I didn't realise you had to qualify the package when running it.

This seems like an awful lot more typing..!

Thanks again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

For reference, here's the documentation.

java - the Java application launcher...
The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method...

... A fully-qualified class name should be used.

JLS - 6.7 Fully Qualified Names and Canonical Names...
  • The fully qualified name of a top level class or top level interface that is declared in an unnamed package is the simple name of the class or interface.
  • The fully qualified name of a top level class or top level interface that is declared in a named package consists of the fully qualified name of the package, followed by ".", followed by the simple name of the class or interface.


  • (PS: Glad to see another Mac user here! )
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    Originally posted by marc weber:
    (PS: Glad to see another Mac user here! )


    You are not alone.


    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
     
     
    subject: Basic CLASSPATH problem on Mac OS X