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
Solved

Basic java question - Calling a class from a class

Posted on 2011-09-07
9
177 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:jkteater
  • 8
9 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36498359

You need to make sure hwne you compile and execute Class1 that you have
the directory where you have Class2 present in the classpath
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36498366
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36498386

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  Class1.java

and the same during execution:

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

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:jkteater
ID: 36498426
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
0
 
LVL 47

Accepted Solution

by:
for_yan earned 125 total points
ID: 36498430
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

package com.company.pack;

then you should place you Class2.java into the folder

c:\foldres_path\B\com\company\pack

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

Expert Comment

by:for_yan
ID: 36498439

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

Expert Comment

by:for_yan
ID: 36498449


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

Class2.ediBaseDialog();  

Class2 is obviously the name of your class conatining this static method
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36498455
In this case if Class2 will be accessible through CLASSPATH as I mentioned above it will execute static method of Class2
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36498480
If
ediBaseDialog();  
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace a word with other 1 44
servlet filter example 37 71
Tomcat: Unable to run tomcat service. 2 23
jsp insert to database example 2 43
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

791 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