aspose file tools*
The moose likes Beginning Java and the fly likes classpath and packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classpath and packages" Watch "classpath and packages" New topic
Author

classpath and packages

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
in question

Q12. If a class is located in a package, what do you need to change in the OS environment to be able to use it?

A. You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
c:\>java com.xyz.hr.Employee



my question is why we have to put into classpath like c:\dev
just because this will be in a classpath variable and we i type
c:\>java com.xyz.hr.Employee

it will take c:\dev from variable

but in other case like
i have develop class name "MyClass.java" and put in packages package c
as c folder is in a/b/
so the final location of MyClass.java is d:\a\b\c\MyClass.java

but its not compiling if i comiple from d:

like D:\>javac a.b.c.MyClass.java
nor it reading it...

why ?


Thanks and Regards, Amit Taneja
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8964
    
    9

Originally posted by amit taneja:

D:\>javac a.b.c.MyClass.java


You aren't supposed to pass the package name to the compiler. Just the file name. The compiler will use the classpath to find source files. Read the tool documentation for how that works.


[How To Ask Questions On JavaRanch]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
but how in original post ...its written that

In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows:
c:\>java com.xyz.hr.Employee



here he is also stroing part of the full addres to classpath like he is storing classpath=c:\java
and how its automatically be finding that ???


and one more thing in the eg.

have develop class name "MyClass.java" and put in packages package c
as c folder is in a/b/
so the final location of MyClass.java is d:\a\b\c\MyClass.java

but its not compiling if i comiple from d:

like D:\>javac a.b.c.MyClass.java


EVEN THOUGH I PUT CLASSPATH=D:\;
AS PER THE IDEA OF ABOVE ORINAL EG.
ITS STILL NOT COMPILING OR ACCESSING ...

..SOLVE THIS COMPLICATION
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
java a.b.c.MyClass

This doesn't work because it says to try to find MyClass in a package named a.b.c, however MyClass is in package c. In order to run it as you have described, you should use

java c.MyClass

and put D:\a\b in the CLASSPATH. If you really want to run it with

java a.b.c.MyClass

then you need to put MyClass in the a.b.c package, not just in the c package.

I hope this helps.

Layne


Java API Documentation
The Java Tutorial
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
In addition to the above, you can compile your java file using

javac -d .\a\b a\b\c\MyClass.java (this will place the class file in a\b\c)

and run MyClass with

java -cp a\b c.MyClass
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classpath and packages