[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Importing my own class files

Posted on 2002-04-16
7
Medium Priority
?
188 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
[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
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
Independent Software Vendors: 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!

 

Accepted Solution

by:
jodear earned 150 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

650 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