• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

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.
0
Tacobell777
Asked:
Tacobell777
4 Solutions
 
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
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!

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now