Solved

Importing my own class files

Posted on 2002-04-16
7
186 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
Technology Partners: 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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

691 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