Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

import problem

Posted on 2004-04-01
20
Medium Priority
?
292 Views
Last Modified: 2013-11-23
Very easy question. Can someone tell me how to import a class which resides in a folder outside current folder?

i have a Abc.java in abc folder, and i want to import cde.java which resides in cde folder, and cde folder and abc folder both resides in def folder.

0
Comment
Question by:jtcy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 3
  • 2
  • +3
20 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738573
Add the folder to the class-path.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738582
Add the folder cde to the class-path, I mean.
0
 

Author Comment

by:jtcy
ID: 10738586
how?
import cde/cde; ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Accepted Solution

by:
Mayank S earned 600 total points
ID: 10738607
In the command-prompt:

set classpath=%classpath%C:\DEF\CDE;.

(assuming that the cde folder is in c:\def - you need to give the full-path for cde).

http://www.mindprod.com/jgloss/classpath.html
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 600 total points
ID: 10738616
>> import cde/cde; ?

Do that only if cde.java is in a package. Have you specified any package for it? Like:

package cde ;

class cde
{
}

- or does it not have a package?
0
 

Author Comment

by:jtcy
ID: 10738626
my directory:

abc folder - contains abc.java labelled package abc
cde folder - contains cde.java labelled package cde

and i wan cde to import abc folder.
do i do like..import abc/abc.java? would it work? cuz they are two different folders

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738629
>> package cde ;

If it is in a package, then add only its parent directory to the class-path.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 600 total points
ID: 10738639
- abc folder should contain abc.class

- cde folder should contain cde.class

Use: import abc ; for importing it

Use: import def ; for importing it

Set the classpath till the parent directory of these two.

set classpath=%classpath%;C:\DEF;.

- if C:\DEF contains these two folders.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738773
Can we have it closed now?
0
 
LVL 92

Expert Comment

by:objects
ID: 10738785
> do i do like..import abc/abc.java?

use:

import abc.abc;

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10738823
>> Use: import abc ; for importing it

Sorry, that should have been:

import abc.* ; // for all classes

or import abc.abc ; // only for this class.
0
 
LVL 1

Expert Comment

by:raghav9992003
ID: 10739013
use:package cde;
in your abc.javafile

so that u can access that java file

try it
0
 
LVL 1

Expert Comment

by:raghav9992003
ID: 10739027
package cde;
 importing packages. if any other things are needed.
public class abc
{
//main method and other code
in this code u can create instance for cde.java file and use it
}
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10739091
>abc folder - contains abc.java labelled package abc
>cde folder - contains cde.java labelled package cde

abc
| - abc.java

after you compiled it (javac -d . abc.java), result will be

abc
| - abc.java
| - abc
     | - abc.class

and also for cde.java

cde
| - cde.java
| - cde
     | - cde.class

To import abc class into cde.java, you need to compile abc.java first (javac -d . abc.java)
, then add <abs path>\abc (that contains abc.java) directory into your classpath (as mayankeagle commented)
, and then compile cde.java.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739122
>> use:package cde;
>> in your abc.javafile

No way. He wants it to be in another package abc.

jtcy,

You asked the same question at:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20941000.html

- please don't ask the same question twice - that too, with the same subject! Since that one is closed, I'm expecting this one to be closed too. Or if you have any more doubts, then please ask them on THIS page since this is still open, and not on that page.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739131
I hope you have given notice to my previous comment:

>> - abc folder should contain abc.class
>> - cde folder should contain cde.class

- Mind it. Its .CLASS file and not .JAVA file

>> Set the classpath till the parent directory of these two

- keep in mind like gnoon suggested that the parent directory need not be the same. In that case, you have to add both the parent directories to the class-path
0
 

Author Comment

by:jtcy
ID: 10739441
Sorry, i didnt want to open two questions. I accidentally clicked twice of it. tats why. um~~~ regarding to this problem i donno`~ basically, i just want to go for a coke..but cant. lol

um.....Okay, as what i have said above.........I have:

folder ast - containing ast classes files, each are packaged as ast;
folder rclast - containing rclast classes files, each packaged rclast;

both folders exists in myDirectory.

NOW, the thing is I want to use ast classes stuff in rclast classes, but i was told to do this without changing anything of classpath.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739480
Why would you want to do that without class-path? Did your teacher not tell you what classpath is?

It is very vital to Java: http://www.mindprod.com/jgloss/classpath.html

What you can try is: put your ast and rclast folders (containing the class-files) in the jdk\bin folder. See if that helps.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10739485
>> I accidentally clicked twice of it.

Hereafter, you can take care of it: you can click on the Delete link which will be available above the Comment box if no comments have been posted to your question as yet.
0
 
LVL 14

Assisted Solution

by:StillUnAware
StillUnAware earned 150 total points
ID: 10739498
One more thought:

if you want to use your own classes in different directories without setting the CLASSPATH, then you cal put all your folders, classes, packages, etc. into folder %JRE_INSTALL%\CLASSES\  where %JRE_INSTALL%\ is the directory where java runtime installation is.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question