Basic java question - Calling a class from a class

I am writing class 1 and it sets in directory A.  With in class 1 I need to call class 2 and it sets in directory B

I am not sure how to specify in my class 1 to run class 2
Who is Participating?
If you have package statement in Class2 then it should be located in certain folder relative to the folder
mentioned in CLASSPATH - and the relative location is determined by package

So if say Class2 satrts with the line


then you should place you into the folder


(assuming you are using the same command as above).

You need to make sure hwne you compile and execute Class1 that you have
the directory where you have Class2 present in the classpath
CLASSPATH can be defined either by enrinement variable CLASSPATH
or using swich -cp before your class name  on command line - both for javac and for java
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Make sure that neither Class1 nor Class2 have package statenemnet on top.
If so then if your defaiult folder is A
then in the command you can say:

C:\path\A> javac -cp .;c:\foldres_path\B

and the same during execution:

C:\path\A>java -cp .;c:\foldres_path\B  Class1
I assume that you are on Windows

jkteaterAuthor Commented:
The classpath is ok - I am having problems with the code

 public Object execute(final ExecutionEvent event) throws ExecutionException {
      Display.getDefault().asyncExec(new Runnable() {
         public void run() {
            ediBaseDialog();   <===  ediBaseDialog is a class sitting in dir B        

Open in new window

I get a  error saying that ediBaseDialog is undefined

 ediBaseDialog();   - is not a class - it is a method - is irt a static maethod or not ?

if it is staic method then you should clali this way:


Class2 is obviously the name of your class conatining this static method
In this case if Class2 will be accessible through CLASSPATH as I mentioned above it will execute static method of Class2
is an instance metyjhod, then you first need to create instance of Class2, say tlike this

Class2 cl2 = new Class2();
cl2. ediBaseDialog();  

But in this case maybe you need to use some other constructore - maybe it requires some parameters,
but anyway if it is an instance method of Class2, then you first have to create an instance of the Class2 object
and then invokke method on that instance
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.