Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic CLASSPATH problem on Mac OS X

 
Alex Hillel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alex Hillel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 1970
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by marc weber:
    (PS: Glad to see another Mac user here! )


    You are not alone.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic