We help IT Professionals succeed at work.

import problem

jtcy
jtcy asked
on
337 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.

Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Add the folder to the class-path.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Add the folder cde to the class-path, I mean.

Author

Commented:
how?
import cde/cde; ?
Principal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> package cde ;

If it is in a package, then add only its parent directory to the class-path.
Mayank SPrincipal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Can we have it closed now?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> do i do like..import abc/abc.java?

use:

import abc.abc;

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> Use: import abc ; for importing it

Sorry, that should have been:

import abc.* ; // for all classes

or import abc.abc ; // only for this class.
use:package cde;
in your abc.javafile

so that u can access that java file

try it
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
}

Commented:
>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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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:

https://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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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.

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.