Solved

Importing my own class files

Posted on 2002-04-16
7
183 Views
Last Modified: 2013-11-23
I want to be able to import a class so that I can use it's methods that sombody else has created.

1. In which directory do I copy the class file so that JAVAC can see it.
2. What do I put in import statement.

Thanks.
0
Comment
Question by:ltdang7
7 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6946889
if you want to import only a class and not a package structured class, you must put that class at the same level with your package start point. The import statement is import aClass.
Suposing you whish to use a class A.class which is not part of any package. You have two cases :
1.your project is package structured, A.class should fit like this:

  c:\Project
            A.class
            \com
                \project
                        ...classes
2. not package structured
   c:\Project\
               A.class
               ...your classes


The simpliest way is to put the class (archive) in the classpath at both compilation and execution. Supposing you have following structure :

c:\Project
          \lib
              A.class
              xyz.jar
           ...your classes or package structure

the compilation&execution of your classes should be done like :

javac -classpath .;c:\Projects\lib\xyz.jar;c:\Projects\lib\A.class; *.java

java -cp c:\Projects\lib\xyz.jar;c:\Projects\lib\A.class; YourClass
0
 
LVL 92

Expert Comment

by:objects
ID: 6946908
The classpath should include the directory, NOT the class name:

javac -classpath c:\Projects\lib\xyz.jar;c:\Projects\lib\ *.java

java -cp c:\Projects\lib\xyz.jar;c:\Projects\lib\ YourClass
0
 
LVL 92

Expert Comment

by:objects
ID: 6946912
And if your sources are in c:\Projects then you may want to use the -d option during compilation so the class files are placed in the lib directory:

javac -d c:\Projects\lib -classpath c:\Projects\lib\xyz.jar;c:\Projects\lib\ *.java

Or alternately include c:\Projects in your classpath.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Accepted Solution

by:
jodear earned 50 total points
ID: 6947007
To answer your questions:

for #1)  In which directory do I copy the class file so that JAVAC can see it.
Ans: Copy your class file into the <Java Home>/lib subdirectory

for #2. What do I put in import statement.
Ans:  import <class name>;
Example: if your class filename is foo.class, the import statement is:

import foo;
0
 
LVL 92

Expert Comment

by:objects
ID: 6947034
> Copy your class file into the <Java Home>/lib

Not only is this unnecessary but the classes will also not be seen by javac if placed in this directory. Except of course you explicitly include this directory in your classpath, but this is true of ANY subdirectory.
0
 
LVL 92

Expert Comment

by:objects
ID: 6947042
If the class is not a part of a package then the easiest thing to do is put it in the same directory as the source files you are compiling.
0
 
LVL 1

Author Comment

by:ltdang7
ID: 6947514
Worked, thanks.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

832 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