Kunal Gaurav
Kunal Gaurav

Reputation: 35

Import user defined package in java from outside of the subfolder

I have created a package in c:\world and I want to import it to my java source file stored in d:\java. It says that unable to access the package c:\world\Balance.class. What do i need to do??

Upvotes: 1

Views: 3916

Answers (3)

pb2q
pb2q

Reputation: 59607

You'll need to have the location of the imported classes on your classpath when compiling your classes and running your code.

From your description and comment, it sounds like you have a package named world with a class named Balance, with Balance.class in c:\world. This should work from d:\java:

javac -cp %CLASSPATH%;c:\ SomeClass.java

Replace SomeClass.java with the name (one or more) of the classes that you're trying to compile in d:\java.

You'll also need to have c:\ in the classpath when you run your code.

Upvotes: 2

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You have 2 ways to solve this problem:

  1. Make the packages available in a single project, this means, both packages would be in the same source directory.

  2. Make a jar that contains your world package. Copy this jar to your lib folder in your project and add it to the classpath, now the world.Balance class can be reached in your current project.

Upvotes: -1

user
user

Reputation: 6947

If you are seeing what I think you are seeing (no c:\world in the specific error message), then it's easy. You need to add c:\world to your CLASSPATH setting.

After that, a simple import Balance (or whatever) should suffice.

Otherwise, a MWE (Minimal Working Example) illustrating the issue, and the exact error message you are getting, would be helpful.

Upvotes: 0

Related Questions