Solved

Importing my own class files

Posted on 2002-04-16
7
181 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pairstar challenge 2 42
endX challenge 2 50
pairs challenge 5 45
reasons why a "inside Menu" would not appear for a web server 6 45
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now