• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thinking in Java 3e, chapter 5, classpath woes  RSS feed

 
Alex Hillel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. No doubt you've all answered a similar question many times over, and I have tried looking for an answer everywhere on the internet, but I can't seem to get it working.

I initially ran into problems with Exercise 4 of Chapter 5, Thinking in Java 3rd Edition, and eventually gave up on the whole set of exercises only to run into the same problem with Exercise 15 of Chapter 6.

I am sure I've got my CLASSPATH set okay, (with the book's example code located at C:\java), it's:

.;C:\Program Files\QuickTime\QTSystem\QTJava.zip;C:\Program Files\Java\jdk1.5.0_07\bin;C:\java

My answers to the exercises are organised in a systematic way, with the .java and .class files of Exercise 4 of Chapter 5 located in C:\java\com\alexhillel\c05a\q04\

My answer to this exercise is made up of two .java files, each containing a class each, with P.java in package com.alexhillel.c05a.q04 (which I assume is right based on my directory structure), and Ptest.java importing that package so as to use the P class. The two files are as follows:

//: P.java
package com.alexhillel.c05a.q04;

public class P {
public static void rint(String s) {
System.out.print(s);
}
public static void rintln(String s) {
System.out.println(s);
}
public static void rint(int i) {
System.out.print(i);
}
public static void rintln(int i) {
System.out.println(i);
}
public static void rint(long l) {
System.out.print(l);
}
public static void rintln(long l) {
System.out.println(l);
}
public static void rint(float f) {
System.out.print(f);
}
public static void rintln(float f) {
System.out.println(f);
}
public static void rint(double d) {
System.out.print(d);
}
public static void rintln(double d) {
System.out.println(d);
}
public static void rint(char c) {
System.out.print(c);
}
public static void rintln(char c) {
System.out.println(c);
}
public static void rint(short sh) {
System.out.print(sh);
}
public static void rintln(short sh) {
System.out.println(sh);
}
public static void rintln(boolean b) {
if(b)
System.out.println("Boolean was true. ");
else
System.out.println("Boolean was false. ");
}
} ///:~


//: Ptest.java
import com.alexhillel.c05a.q04.*;

public class Ptest {
public static void main(String[] args) {
P.rintln("This is a string.");
}
} ///:~


Now P.java compiles okay, but when I compile Ptest.java I get the following error message:

C:\java\com\alexhillel\c05a\q04>javac Ptest.java
Ptest.java:5: cannot access P
bad class file: .\P.class
class file contains wrong class: com.alexhillel.c05a.q04.P
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
P.rintln("This is a string.");
^
1 error


Why should this happen? I seem to have my package names correct... or do I?

Many thanks in advance,
Alex
 
Pete Thorson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you want to do the compile from C:\Java so that the Class files are found in the correct packages. From the error I'm guessing that the compiler goes looking for P with no package and when it finds P with a package specified it errors out.
 
Alex Hillel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! It did work when I compiled it from C:\java. However I had to go back into C:\java\com\alexhillel\c05a\q04 before it'd let me run the program. Is there any way of it allowing me to compile from C:\java\com\alexhillel\c05a\q04 so I don't have to keep swapping about directories on the command line?

Thanks again.
 
Pete Thorson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the easiest thing is to get rid of the package and import statements in P and Ptest respectively. You shouldn't need the import since the class fiels are in the same directory and if you are compiling from C:\java\com\alexhillel\c05a\q04\ you don't need the package either.

On the other hand, you should probably have been able to run the Ptest from C:\Java
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could leave PTest where it is and add a package statement to PTest placing it in the same package as P and remove the import statements and run it as:
java com.alexhillel.c05a.q04.PTest

Barring that you have to remove PTest from the folder you have it in and put it in the base directory (C:\java). This way you must keep the import statements and you can run the program with:
java PTest
 
Alex Hillel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I was attempting to run it with "javac c:\java\com\alexhillel\c05a\q04\Ptest.java" rather than "java com.alexhillel.c05a.q04.Ptest".

All makes sense now, thanks guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!