Hi all,

In my project I have several classes. In some of my classes, I have to use an instances of my other classes. When I compile my class, which has a reference to another classe, it gives me an error. like this.

C:\MyProject>javac SomeClass.java
SomeClass.java:9: cannot resolve symbol
symbol  : class AnotherClass
location: class MyProject.SomeClass
public SomeClass (AnotherClass as) {

For example I have the following class.

import java.io.*
// other imports

public class SomeClass {
       public SomeClass(AnotherClass ac) {
                this.as = as;
       }
       
       private someMethod () {
       }

       private AnotherClass ac = new AnotherClass();
}

and here's the my AnotherClass.

import java.io.*;
// other imports

public class AnotherClass {
        public AnotherClass () {
        {

        private someMethod () {
        }
}

All my classes are under the same package

C:\MyProject\SomeClass.java
C:\MyProject\AnotherClass.java
and so on....


So, How do I need to include the package? Or if I need to include the package? Can some one please suggest.


Thanks.

Try placing all the files of your package in a directory named <PackageName>. This directory can branch off from any directory named in the CLASSPATH environment variable. You may find this article to be useful. Hope this helps..

Try placing all the files of your package in a directory named <PackageName>. This directory can branch off from any directory named in the CLASSPATH environment variable. You may find this article to be useful. Hope this helps..

Thanks very much for this. It worked. :o)


But now I can compile all the classes alright; however, when I try to run my program as follow: I get the following error message.

F:\DCTM_WORK>java FlightReservationDesktop\Reservation
Exception in thread "main" java.lang.NoClassDefFoundError: FlightReservationDesktop\Reservation (wrong name: FlightReservationDesktop/Reservation)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

and this is the value of my classpath

C:\>echo %CLASSPATH%
C:\Program Files\Documentum\dctm.jar;C:\Documentum\config;C:\Documentum\product\5.3\dctm-server.jar;C:\Program Files\Documentum\Shared;C:\Program Files\Documentum\Shared\admin.jar;F:\DCTM_WORK\FlightReservationDesktop;.

I have the "main" method in my class "Reservation".

Can you please suggest...

Thanks.

include the directory in the classpath for the run by using the -classpath parameter to the java commmand.

I still get the same error: Here's how I tried:

F:\DCTM_WORK>java -classpath "FlightReservationDesktop" Reservation
Exception in thread "main" java.lang.NoClassDefFoundError: Reservation (wrong name: FlightReservationDesktop/Reservation)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

I also tried running it from the directory that contains my classes, with same error:

F:\DCTM_WORK\FlightReservationDesktop>java -classpath "." Reservation
Exception in thread "main" java.lang.NoClassDefFoundError: Reservation (wrong name: FlightReservationDesktop/Reservation)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Please advise...

Thanks.

Bah, my bad, that should be a period - not a slash - to denote the package of the class. This is what it should be

F:\DCTM_WORK>java FlightReservationDesktop.Reservation
This question has already been answered. Start a new discussion instead.