Need help in CLASSPATH/Package  
Author Message
linuxisez





PostPosted: 2004-7-6 7:12:00 Top

java-programmer, Need help in CLASSPATH/Package hi everyone,

i am having a strange problem regarding package/classpath.
Very Sorry, Its a long post but please help.

I did like this:

I created MyMath.java file in C:\Java , the contents were:

package com.anbh.Maths;
public class MyMath {
public static int add(int x, int y) {
return x + y;
}

public static int sub(int x, int y) {
return x-y;
}
}

Created file UrMath.java again in C:\Java, contents were :

package com.anbh.Maths;

public class UrMath {
public String callMe() {
return "Inside CallMe()";
}
}

Created file TempClass again in C:\Java, Contents were

import com.anbh.Maths.*;

public class TempClass {
public static void main(String[] args) {
System.out.println(MyMath.add(-1, -1));
UrMath ob = new UrMath();
System.out.println(ob.callMe());
}
}

Then I compiled it like below: (My current directory was C:\Java)

javac -d . MyMath.java UrMath.java

This Complied successfully and created directories C:\Java\com\anbh\Maths.

Then I tried to compile TempClass.java like: (My current directory was C:\Java)

javac -classpath . TempClass.java

I got the error:

TempClass.java:6: cannot access MyMath
bad class file: .\MyMath.java
file does not contain class MyMath
Please remove or make sure it appears in the correct subdirectory of the classpath.
System.out.println(MyMath.add(-1, -1));
^
1 error

Now, when I changed the TempClass.java file's import statement as:-

import com.anbh.Maths.MyMath;
import com.anbh.Maths.UrMath;

i.e. I named every class individually, EVERYTHING WORKED FINE.

Why import com.anbh.Maths.* notation is not working???????

Please help, I had wasted 1 day on this.
Thanks in adv.
 
Roedy Green





PostPosted: 2004-7-6 7:35:00 Top

java-programmer >> Need help in CLASSPATH/Package On 5 Jul 2004 16:12:29 -0700, email***@***.com (Anks) wrote or
quoted :

>package com.anbh.Maths;

first problem. Package names are always lower case.

See http://mindprod.com/jgloss/package.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Roedy Green





PostPosted: 2004-7-6 7:38:00 Top

java-programmer >> Need help in CLASSPATH/Package On 5 Jul 2004 16:12:29 -0700, email***@***.com (Anks) wrote or
quoted :

>I created MyMath.java file in C:\Java ,

No. Your package name constrains where you put the source file.

see http://mindprod.com/jgloss/package.html

If your package is called com.mindprod.bulk
and your Class is called Eliminator

Then the java source file must be in

C:\com\mindprod\bulk\Eliminator.java

or some other directory tree that ends that way.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
linuxisez





PostPosted: 2004-7-7 19:32:00 Top

java-programmer >> Need help in CLASSPATH/Package Thanks for answering but, it is NOT constrained where to put the
*.java file, if it has been comipled to a *.class file. If u take note
in my original post I had already put the *.class files in the proper
directory tree (by the command javac -d . etc)

Now when the class files are in the proper place, then why import with
* notaion is not working.

Regards.
Anks
 
 
linuxisez





PostPosted: 2004-7-7 19:59:00 Top

java-programmer >> Need help in CLASSPATH/Package Sincere apologies!!!

Your solution worked. Thanks and ignore my previous post please. But
can u please explain the reason exactly as to why put the source files
within the package directory. This makes the directory clubbed with
*.class and *.java files.

The way I tried to compiled was LOGICALLY right J. Why java compiler
is picking up wrong files.

What if I do not want to club class and java file together. Please
help.

Regards,
Anks.
 
 
Andrew Thompson





PostPosted: 2004-7-7 20:33:00 Top

java-programmer >> Need help in CLASSPATH/Package On 7 Jul 2004 04:58:48 -0700, Anks wrote:

> What if I do not want to club class and java file together. Please
> help.

The ANT build tool can do all sorts of amazing things,
but your do not need to go to that level of complexity
to achieve a separation of source and classes.
<http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/javac.html#options>
See the '-d' option

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
linuxisez





PostPosted: 2004-7-8 4:05:00 Top

java-programmer >> Need help in CLASSPATH/Package Hi Andrew Thompson,

Thanks for the response. If you take note in my initial post (1st post
of this thread), u would see that i had already used -d option. In
fact I had already separated *.java and *.class files. But here I was
discussing issue regarding packages.

Why to place *.java files (the source files) in the package directory
when I had put class files in it and these r the files that will be
used by JVM???

Can some one please help me on this.

Regards.
Anks