aspose file tools*
The moose likes Beginning Java and the fly likes Problem in accessing the in my own package. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in accessing the in my own package." Watch "Problem in accessing the in my own package." New topic
Author

Problem in accessing the in my own package.

Vijay Sharma
Greenhorn

Joined: Oct 12, 2005
Posts: 17
Hi,

Am creating a package called com.vijay.simple and the CLASSPATH variable is set to D:\vijay\java\jdk1.4\bin. My working directory is D:\vijay\java\programs\tij.

I executed two java source files List.java and Vector.java in my working directory.

// Creating a package.
package com.vijay.simple;

public class Vector
{
public Vector()
{
System.out.println("Executing default constructor in Vector");
}
} ///:~
---------------------------------------------------------------------------

// Creating a package.
package com.vijay.simple;

public class List
{
public List()
{
System.out.println("Executing default constructor in List");
}
} ///:~
---------------------------------------------------------------------------

// Uses the library.
import com.vijay.simple.*;

public class LibTest {
public static void main(String[] args) {
Vector v = new Vector();
List l = new List();
}
} ///:~
---------------------------------------------------------------------------

When I import this package in main program am getting an error message as below
"LibTest.java:3: package com.vijay.simple does not exist
import com.vijay.simple.*;"

Can anyone please help me out with this problem?


Cheers,<br />Vijay Sharma
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

1) Don't set CLASSPATH to anything. Seriously. Until you understand packages and class loading better, the default is fine, and much better than what you've already got. Remove the current setting altogether.

2) Make directories com\vijay\simple in your current directory. Put the Java files into com\vijay\simple.

3) Compile with javac com\vijay\simple\LibTest.java; it works!

4) DON'T WRITE CLASSES NAMED 'VECTOR' AND 'LIST'! In general, don't reuse the names of common API classes in your code.


[Jess in Action][AskingGoodQuestions]
Vijay Sharma
Greenhorn

Joined: Oct 12, 2005
Posts: 17
Ernest,

Thanks for the suggestion.

I renamed the class to Vector1 and List1 now which doesnt conflicts with the java library classes anymore. I have saved the .class files of both the source files in D:\vijay\java\com\vijay\simple folder.

What am trying to do is I want to import these class files from D:\vijay\java\program\tij folder where my main program is saved. My main program should call the .class in the "simple" folder, where i want to build my own library.

No problem it works by creating a folder com\vijay\simple in my current working directory "tij" containing my .class files in it and execute it with the command u told. But i want it to do in the way i have told above.

Thanks in advance.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
First of all, you should move the .java files to the com\vijay\simple subfolder so that you can easily recompile any changes that you make to them. Using these classes from another folder is identical to using classes from the standard Java API. You need an appropriate import statement to make the classes available. Have you tried this yet? If so, what does your "main" class look like? Where is it saved? What happens when you try to compile and run your program?

I ask these questions because I'm not sure where you are having problems. Please provide these details so that we can help you.

Layne


Java API Documentation
The Java Tutorial
Vijay Sharma
Greenhorn

Joined: Oct 12, 2005
Posts: 17
Hi Layne,

First of all I would thank you for your reply.

1. I have saved my Vecotor1 and List1 classes in the path D:\vijay\java\com\vijay\simple.

2. The import statement is import com.vijay.simple.*

3. My main program looks like below.

// Uses the library.
import com.vijay.simple.*;

public class LibTest {
public static void main(String[] args) {
Vector1 v = new Vector1();
List1 l = new List1();
}
}

The main program stored in the path D:\vijay\java\programs\tij directory. My class path variable is set to both the \bin and D:\vijay\java\com\vijay\simple where my Vector1 and List1 classes resides, but still am getting the error the package com.vijay.simple doesnot exists.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Vijay, as I told you before

UNSET CLASSPATH!

There's a reason I give that advice: because it works. If you choose not to follow it, then don't be surprised when other things don't work! The reason you're having so much trouble is because you've set the CLASSPATH, and you've set it wrong.

The CLASSPATH environment variable is supposed to point to the roots of the package hierarchies of your classes, or to JAR files. Because "com" is located in D:\vijay\java, it is D:\vijay\java that belongs on the classpath, not D:\vijay\java\com\vijay\simple. The latter will not work.

Further, JDK\bin never, ever belongs on the CLASSPATH. Not for beginners, not for advanced users, not for anyone. There is never any reason, ever, to put this directory on the CLASSPATH. There are no classes in that directory, nor should there be.

Now. If CLASSPATH is not set at all, then the default is just the current directory. That means that no matter where your current directory is, you can easily work with the classes there. That's why I tell newbies not to set CLASSPATH at all.

If you must set it, because for some reason setting it makes you happy, then you must include "." (a dot, a period) as one of the entries. This dot means "the current directory." Then the default is included as part of whatever else you add, and again, this will work.
Vijay Sharma
Greenhorn

Joined: Oct 12, 2005
Posts: 17
Hi Erenst,

I followed your instruction and it worked. . Also I tried creating Jar file and it worked too cheers for your help and keep guiding the beginners in future.

Lastly I really thank you for your help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem in accessing the in my own package.