File locations

I am new to Java, I am hoping someone can tell me where to put my files;

*.java
*.class

And how do I make sure it finds classes it needs to import when I compile the code.
LVL 17
Tacobell777Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tomgalloCommented:
In which enviroment are you working on?
In my case I'll create serveral folders for example:

First of all I'll create this folders on the same level:
-classes
-src

Then I'll put all *.java in subroot "src" and all classes in subroot "classes" with this directory structure inside for each subroot:

com->myproject->classname-> (classname.java or classname.class)

The same structure for both subroots. Is an easier approach to create packages.
Hope that helps.
-tom
0
tomgalloCommented:
To use the .class you have to add in the CLASSPATH the path of the classes that you want to use. As a java standard you have to create a subdir for each class
-tom
0
tomgalloCommented:
Another comment,

If you have 5 .java that are classes inherited from another, you can do this, for example:

Project: Tree -

The first structure will be like this:
src->com->tree->fathertree->FatherTree.java

Suppose that you have 3 more classes inherited from fathertree then you will do:
src->com->tree->fathertree->sontree->SonTreeA.java
src->com->tree->fathertree->sontree->SonTreeB.java
src->com->tree->fathertree->sontree->SonTreeC.java

You'll put the .classes under the classes subroot. As a naming convention in java all the class files begin with a capital.

-tom

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

NaeemgCommented:
use JBuilderX Enterprise version.
in JBuilderX when u import packages and classes.
and if they are not used in class then their color becomes gray. otherwsie black.
0
tomgalloCommented:
Well, you have to add the subdirs to the classpath, for example:
I have this class: src->com->hydranet->mailer->Mailer.class (or Mailer.java)

If I want to import it, I'll put in my classpath the path "src" then I'll declare the import this way: import com.hydranet.mailer.Mailer;

Is good to have all the structure in the import because you can see the inherited classes in it.

-tom
0
jprgnCommented:
The best way to do is to follow these steps:

 1. create a package by putting all the use full classes which you want to import in the package.
 2. compile the complete package.
 3. create jar of that package.

    jar -cvf jarfilename.jar packagename.*;

 4. set the -classpath for that .jar file before compiling ur code in which ur importing that package or required classes.

 5. Run your code

if you still got any confusion then don't hesistate to ask
0
vikraman_bCommented:
Hi
download eclipse..
it will prevent compile errors also it provides necessary sugeston to avoid errors on double clicking the icon
It manages import on click(it automatically imports the necessary package.
try this
http://mirror.tiscali.dk/eclipse/downloads/
u dont need to worry about that..it will show the icon in yellow color on doubleclick..it will automatically import for u...good one
ALso
for ur problem...

first set the class path for java
path=ur jdk path/bin (e.g D:\bea\jdk141_02\bin)
classpath=ur jdk path/lib (e.g D:\bea\jdk141_02\bin)

create package for eg
com.test.maint;
under this package u got one class Main.java
and one more package for eg
com.test.error;
under this u got one more class Error.java and u  got one static method called getE()

In the Main.java if u want to use this getE() of Error class

u need to code
Package com.test.maint;
import com.test.error.Error;
ublic class Main{
public void doX(){
Error.getE();
}
}

Let these two classes lies in D:/test/Main.java
                                                  /Error.java

If u compile like D:/test/javac -d . *.java
-d for package options and . for current directory
Instead of . u can specify the path where u want to put the class files.

the generated class files will be under
D:/test/com/test/maint/Main.class
D:/test/com/test/error/Error.class

for u want to use external jar use classpath option

ANy doubts ?




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.