ptpare
September 7th, 2010, 09:25 AM
I am trying to learn the JAVA programming language but I am finding it very difficult to get past first base.
The problem seems to be with the CLASSPATH setting.
I am running Linux Ubuntu Hardy Heron and I have included some of my terminal output below.
I have eventually managed to compile SIUnits, List.java, Text.java, Graph.java and Display. java from Judith Bishop's book, Java Gently for Engineers and Scientists, but I can not get TrainTravel.java or any of the other programes in Chapter 2 of the book to compile.
I received the following output:
bad class file: /home/phillip/java/javagently/Text.class
class file contains wrong class: javagently.Text
Please remove or make sure it appears in the correct subdirectory of the classpath.
("The journey covers " + Text.format(totalDistance,5,2) + " km");
^
1 error
I have included My session history, the Text.java file and the TrainTravel.java file below
I was redirected to this forum after having initially posted in the Absolute Beginner forum.
Would Netbeans be an easier way to learn Java? I tried a Windows version of Netbeans but even there I had difficulties in getting everything to compile.
Would Netbeans also be able to work with the OpenJDK or would I have to use the Sun JDK?
I did implement the following instructions:
Starting with java-common 0.23ubuntu1, the update-java-alternatives
script can be used to set a bunch of jre/jdk alternatives:
- Set all runtime tools to point to the openjdk-6 alternatives:
update-java-alternatives --jre --set java-6-openjdk
- Set all runtime tools (headless only) to point to the openjdk-6
alternatives:
update-java-alternatives --jre-headless --set java-6-openjdk
- Set all runtime and development tools to point to the openjdk-6
alternatives:
update-java-alternatives --set java-6-openjdk
- Set all runtime and development tools to auto mode:
update-java-alternatives --auto
Any help will be appreciated.
Thank you.
Regards
Phillip
I found the JDK version that I am using as follows:
phillip@voogdy-study:~/java/myprograms/ch2$ java -version
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1~8.04.3)
OpenJDK Client VM (build 16.0-b13, mixed mode, sharing)
phillip@voogdy-study:~/java/myprograms/ch2$
My session history:
phillip@voogdy-study:~$ echo $CLASSPATH
phillip@voogdy-study:~$ export CLASSPATH=~/java/:.
phillip@voogdy-study:~$ echo $CLASSPATH
/home/phillip/java/:.
phillip@voogdy-study:~$ javac Graph.java
javac: file not found: Graph.java
Usage: javac <options> <source files>
use -help for a list of possible options
phillip@voogdy-study:~$ cd java
phillip@voogdy-study:~/java$ cd javagently
phillip@voogdy-study:~/java/javagently$ ls
Display.java List.class List$Node.class Text.java
Graph.java List.java Text.class
phillip@voogdy-study:~/java/javagently$ javac Graph.java
phillip@voogdy-study:~/java/javagently$ export CLASSPATH=$CLASSPATH:~/java/javagently/
phillip@voogdy-study:~/java/javagently$
phillip@voogdy-study:~/java/javagently$ echo $CLASSPATH
/home/phillip/java/:.:/home/phillip/java/javagently/
phillip@voogdy-study:~/java/javagently$ ls
Display.java Graph.java List.java Text.java
phillip@voogdy-study:~/java/javagently$ cd ..
phillip@voogdy-study:~/java$ cd ..
phillip@voogdy-study:~$ javac text.java
javac: file not found: text.java
Usage: javac <options> <source files>
use -help for a list of possible options
phillip@voogdy-study:~$ echo $CLASSPATH
/home/phillip/java/:.:/home/phillip/java/javagently/
phillip@voogdy-study:~$ cd java
phillip@voogdy-study:~/java$ cd javagently
phillip@voogdy-study:~/java/javagently$ ls
Display.java Graph.java List.java Text.java
phillip@voogdy-study:~/java/javagently$ cd ..
phillip@voogdy-study:~/java$ cd ..
phillip@voogdy-study:~$ javac Text.java
javac: file not found: Text.java
Usage: javac <options> <source files>
use -help for a list of possible options
phillip@voogdy-study:~$ cd java
phillip@voogdy-study:~/java$ cd javagently
phillip@voogdy-study:~/java/javagently$ ls
Display.java Graph.java List.java Text.java
phillip@voogdy-study:~/java/javagently$ javac Text.java
phillip@voogdy-study:~/java/javagently$ javac List.java
phillip@voogdy-study:~/java/javagently$ javac Graph.java
phillip@voogdy-study:~/java/javagently$ javac Display.java
Note: Display.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
phillip@voogdy-study:~/java/javagently$ ls
Display$1.class Display$Watcher.class Graph.java List$Node.class
Display.class Graph$1.class Graph$Point.class Text.class
Display$Data.class Graph.class List.class Text.java
Display.java Graph$Dataset.class List.java
phillip@voogdy-study:~/java/javagently$ cd ..
phillip@voogdy-study:~/java$ cd myexamples
bash: cd: myexamples: No such file or directory
phillip@voogdy-study:~/java$ cd myprograms/
phillip@voogdy-study:~/java/myprograms$ ls
ch10 ch2 ch3 ch4 ch5 ch6 ch7 ch8 ch9
phillip@voogdy-study:~/java/myprograms$ cd ch2
phillip@voogdy-study:~/java/myprograms/ch2$ ls
DisplayWarning.java NewFleet.java SIUnits.class TrainTravel.java
LiquidFlow.java Resistance.java SIUnits.java TrigGraphs.java
phillip@voogdy-study:~/java/myprograms/ch2$ javac TrainTravel.java
TrainTravel.java:40: cannot access Text
bad class file: /home/phillip/java/javagently/Text.class
class file contains wrong class: javagently.Text
Please remove or make sure it appears in the correct subdirectory of the classpath.
("The journey covers " + Text.format(totalDistance,5,2) + " km");
^
1 error
phillip@voogdy-study:~/java/myprograms/ch2$ javac TrigGraphs.java
TrigGraphs.java:15: cannot access Graph
bad class file: /home/phillip/java/javagently/Graph.class
class file contains wrong class: javagently.Graph
Please remove or make sure it appears in the correct subdirectory of the classpath.
Graph g = new Graph("Sine and Cosine","x","y");
^
1 error
package javagently;
import java.io.*;
import java.util.*;
import java.text.*;
This is the Text.java file:
public class Text {
public Text () {};
/* The All New Famous Text class by J M Bishop Aug 1996
* revised for Java 1.1 by Alwyn Moolman Aug 1997
* revised for efficiency by J M Bishop Dec 1997
* revised with renamed output methods
* by J M Bishop in July 1999
*
* Provides simple input from the keyboard and files.
* Now also has simple output formatting methods
* and file opening facilities.
*
* public static void prompt (String s)
* public static int readInt (BufferedReader in)
* public static double readDouble (BufferedReader in)
* public static String readString (BufferedReader in)
* public static char readChar (BufferedReader in)
* public static String writeInt (int number, int align)
* public static String writeDouble
(double number, int align, int frac)
* public static BufferedReader open (InputStream in)
* public static BufferedReader open (String filename)
* public static PrintWriter create (String filename)
*/
private static StringTokenizer T;
private static String S;
public static BufferedReader open (InputStream in) {
return new BufferedReader(new InputStreamReader(in));
}
public static BufferedReader open (String filename)
throws FileNotFoundException {
return new BufferedReader (new FileReader (filename));
}
public static PrintWriter create
(String filename) throws IOException {
return new PrintWriter (new FileWriter (filename));
}
public static void prompt (String s) {
System.out.print(s + " ");
System.out.flush();
}
public static int readInt (BufferedReader in) throws IOException {
if (T==null) refresh(in);
while (true) {
try {
return Integer.parseInt(T.nextToken());
}
catch (NoSuchElementException e1) {
refresh (in);
}
catch (NumberFormatException e2) {
System.out.println("Error in number, try again.");
}
}
}
public static char readChar (BufferedReader in) throws IOException {
if (T==null) refresh(in);
while (true) {
try {
return T.nextToken().trim().charAt(0);
}
catch (NoSuchElementException e1) {
refresh (in);
}
}
}
public static double readDouble (BufferedReader in) throws IOException {
if (T==null) refresh(in);
while (true) {
try {
String item = T.nextToken();
return Double.valueOf(item.trim()).doubleValue();
}
catch (NoSuchElementException e1) {
refresh (in);
}
catch (NumberFormatException e2) {
System.out.println("Error in number, try again.");
}
}
}
public static String readString (BufferedReader in) throws IOException {
if (T==null) refresh (in);
while (true) {
try {
return T.nextToken();
}
catch (NoSuchElementException e1) {
refresh (in);
}
}
}
private static void refresh (BufferedReader in) throws IOException {
S = in.readLine ();
if (S==null) throw new EOFException();
T = new StringTokenizer (S);
}
// Format methods
// --------------
private static DecimalFormat N = new DecimalFormat();
private static final String spaces = " ";
public static String format(double number, int align, int frac) {
N.setGroupingUsed(false);
N.setMaximumFractionDigits(frac);
N.setMinimumFractionDigits(frac);
String num = N.format(number);
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
return num;
}
public static String format(int number, int align) {
N.setGroupingUsed(false);
N.setMaximumFractionDigits(0);
String num = N.format(number);
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
return num;
}
public static String writeDouble (double number, int align, int frac) {
// Deprecated in July 1999 but retained for compatability
return format (number, align, frac);
}
public static String writeInt (int number, int align) {
// Deprecated in July 1999 but retained for compatability
return format (number, align);
}
}
This is the TrainTravel.jar file:
import javagently.*;
class TrainTravel {
/* The Train Travel Program J M and N T Bishop
======================== 1990, 1999
*
* Calculates the distance travelled under acceleration,
* constant speed and deceleration.
* Illustrates constants, variables, expressions and printing
*/
static final double startAcceleration = 0.5; // m/s/s
static final double deceleration = 1.0; // m/s/s
static final double time1 = 2; // min
static final double time2 = 3; // min
static final double kilo = 1000;
// metre to km conversion factor
public static void main (String args []) {
System.out.println("******* Train travel *******\n");
System.out.println(startAcceleration +
" m/s/s decreasing to 0 in " +
time1 + " mins,");
System.out.println("constant speed for " + time2 + " mins,");
System.out.println("and " + deceleration + "m/s/s to rest\n");
// Perform the calculation in simple steps, saving
// each partial result
double secs1 = time1*60; // time1 in secs
double secs2 = time2*60; // time2 in secs
double velocity = 0.5 * startAcceleration * secs1;
double distance1 = (startAcceleration * secs1 * secs1) / 3;
double distance2 = velocity * secs2;
double distance3 = velocity * velocity / (2 * deceleration);
double totalDistance = (distance1 + distance2 + distance3)
/ kilo;
System.out.println
("The journey covers " + Text.format(totalDistance,5,2) + " km");
}
}
The problem seems to be with the CLASSPATH setting.
I am running Linux Ubuntu Hardy Heron and I have included some of my terminal output below.
I have eventually managed to compile SIUnits, List.java, Text.java, Graph.java and Display. java from Judith Bishop's book, Java Gently for Engineers and Scientists, but I can not get TrainTravel.java or any of the other programes in Chapter 2 of the book to compile.
I received the following output:
bad class file: /home/phillip/java/javagently/Text.class
class file contains wrong class: javagently.Text
Please remove or make sure it appears in the correct subdirectory of the classpath.
("The journey covers " + Text.format(totalDistance,5,2) + " km");
^
1 error
I have included My session history, the Text.java file and the TrainTravel.java file below
I was redirected to this forum after having initially posted in the Absolute Beginner forum.
Would Netbeans be an easier way to learn Java? I tried a Windows version of Netbeans but even there I had difficulties in getting everything to compile.
Would Netbeans also be able to work with the OpenJDK or would I have to use the Sun JDK?
I did implement the following instructions:
Starting with java-common 0.23ubuntu1, the update-java-alternatives
script can be used to set a bunch of jre/jdk alternatives:
- Set all runtime tools to point to the openjdk-6 alternatives:
update-java-alternatives --jre --set java-6-openjdk
- Set all runtime tools (headless only) to point to the openjdk-6
alternatives:
update-java-alternatives --jre-headless --set java-6-openjdk
- Set all runtime and development tools to point to the openjdk-6
alternatives:
update-java-alternatives --set java-6-openjdk
- Set all runtime and development tools to auto mode:
update-java-alternatives --auto
Any help will be appreciated.
Thank you.
Regards
Phillip
I found the JDK version that I am using as follows:
phillip@voogdy-study:~/java/myprograms/ch2$ java -version
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8.1) (6b18-1.8.1-0ubuntu1~8.04.3)
OpenJDK Client VM (build 16.0-b13, mixed mode, sharing)
phillip@voogdy-study:~/java/myprograms/ch2$
My session history:
phillip@voogdy-study:~$ echo $CLASSPATH
phillip@voogdy-study:~$ export CLASSPATH=~/java/:.
phillip@voogdy-study:~$ echo $CLASSPATH
/home/phillip/java/:.
phillip@voogdy-study:~$ javac Graph.java
javac: file not found: Graph.java
Usage: javac <options> <source files>
use -help for a list of possible options
phillip@voogdy-study:~$ cd java
phillip@voogdy-study:~/java$ cd javagently
phillip@voogdy-study:~/java/javagently$ ls
Display.java List.class List$Node.class Text.java
Graph.java List.java Text.class
phillip@voogdy-study:~/java/javagently$ javac Graph.java
phillip@voogdy-study:~/java/javagently$ export CLASSPATH=$CLASSPATH:~/java/javagently/
phillip@voogdy-study:~/java/javagently$
phillip@voogdy-study:~/java/javagently$ echo $CLASSPATH
/home/phillip/java/:.:/home/phillip/java/javagently/
phillip@voogdy-study:~/java/javagently$ ls
Display.java Graph.java List.java Text.java
phillip@voogdy-study:~/java/javagently$ cd ..
phillip@voogdy-study:~/java$ cd ..
phillip@voogdy-study:~$ javac text.java
javac: file not found: text.java
Usage: javac <options> <source files>
use -help for a list of possible options
phillip@voogdy-study:~$ echo $CLASSPATH
/home/phillip/java/:.:/home/phillip/java/javagently/
phillip@voogdy-study:~$ cd java
phillip@voogdy-study:~/java$ cd javagently
phillip@voogdy-study:~/java/javagently$ ls
Display.java Graph.java List.java Text.java
phillip@voogdy-study:~/java/javagently$ cd ..
phillip@voogdy-study:~/java$ cd ..
phillip@voogdy-study:~$ javac Text.java
javac: file not found: Text.java
Usage: javac <options> <source files>
use -help for a list of possible options
phillip@voogdy-study:~$ cd java
phillip@voogdy-study:~/java$ cd javagently
phillip@voogdy-study:~/java/javagently$ ls
Display.java Graph.java List.java Text.java
phillip@voogdy-study:~/java/javagently$ javac Text.java
phillip@voogdy-study:~/java/javagently$ javac List.java
phillip@voogdy-study:~/java/javagently$ javac Graph.java
phillip@voogdy-study:~/java/javagently$ javac Display.java
Note: Display.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
phillip@voogdy-study:~/java/javagently$ ls
Display$1.class Display$Watcher.class Graph.java List$Node.class
Display.class Graph$1.class Graph$Point.class Text.class
Display$Data.class Graph.class List.class Text.java
Display.java Graph$Dataset.class List.java
phillip@voogdy-study:~/java/javagently$ cd ..
phillip@voogdy-study:~/java$ cd myexamples
bash: cd: myexamples: No such file or directory
phillip@voogdy-study:~/java$ cd myprograms/
phillip@voogdy-study:~/java/myprograms$ ls
ch10 ch2 ch3 ch4 ch5 ch6 ch7 ch8 ch9
phillip@voogdy-study:~/java/myprograms$ cd ch2
phillip@voogdy-study:~/java/myprograms/ch2$ ls
DisplayWarning.java NewFleet.java SIUnits.class TrainTravel.java
LiquidFlow.java Resistance.java SIUnits.java TrigGraphs.java
phillip@voogdy-study:~/java/myprograms/ch2$ javac TrainTravel.java
TrainTravel.java:40: cannot access Text
bad class file: /home/phillip/java/javagently/Text.class
class file contains wrong class: javagently.Text
Please remove or make sure it appears in the correct subdirectory of the classpath.
("The journey covers " + Text.format(totalDistance,5,2) + " km");
^
1 error
phillip@voogdy-study:~/java/myprograms/ch2$ javac TrigGraphs.java
TrigGraphs.java:15: cannot access Graph
bad class file: /home/phillip/java/javagently/Graph.class
class file contains wrong class: javagently.Graph
Please remove or make sure it appears in the correct subdirectory of the classpath.
Graph g = new Graph("Sine and Cosine","x","y");
^
1 error
package javagently;
import java.io.*;
import java.util.*;
import java.text.*;
This is the Text.java file:
public class Text {
public Text () {};
/* The All New Famous Text class by J M Bishop Aug 1996
* revised for Java 1.1 by Alwyn Moolman Aug 1997
* revised for efficiency by J M Bishop Dec 1997
* revised with renamed output methods
* by J M Bishop in July 1999
*
* Provides simple input from the keyboard and files.
* Now also has simple output formatting methods
* and file opening facilities.
*
* public static void prompt (String s)
* public static int readInt (BufferedReader in)
* public static double readDouble (BufferedReader in)
* public static String readString (BufferedReader in)
* public static char readChar (BufferedReader in)
* public static String writeInt (int number, int align)
* public static String writeDouble
(double number, int align, int frac)
* public static BufferedReader open (InputStream in)
* public static BufferedReader open (String filename)
* public static PrintWriter create (String filename)
*/
private static StringTokenizer T;
private static String S;
public static BufferedReader open (InputStream in) {
return new BufferedReader(new InputStreamReader(in));
}
public static BufferedReader open (String filename)
throws FileNotFoundException {
return new BufferedReader (new FileReader (filename));
}
public static PrintWriter create
(String filename) throws IOException {
return new PrintWriter (new FileWriter (filename));
}
public static void prompt (String s) {
System.out.print(s + " ");
System.out.flush();
}
public static int readInt (BufferedReader in) throws IOException {
if (T==null) refresh(in);
while (true) {
try {
return Integer.parseInt(T.nextToken());
}
catch (NoSuchElementException e1) {
refresh (in);
}
catch (NumberFormatException e2) {
System.out.println("Error in number, try again.");
}
}
}
public static char readChar (BufferedReader in) throws IOException {
if (T==null) refresh(in);
while (true) {
try {
return T.nextToken().trim().charAt(0);
}
catch (NoSuchElementException e1) {
refresh (in);
}
}
}
public static double readDouble (BufferedReader in) throws IOException {
if (T==null) refresh(in);
while (true) {
try {
String item = T.nextToken();
return Double.valueOf(item.trim()).doubleValue();
}
catch (NoSuchElementException e1) {
refresh (in);
}
catch (NumberFormatException e2) {
System.out.println("Error in number, try again.");
}
}
}
public static String readString (BufferedReader in) throws IOException {
if (T==null) refresh (in);
while (true) {
try {
return T.nextToken();
}
catch (NoSuchElementException e1) {
refresh (in);
}
}
}
private static void refresh (BufferedReader in) throws IOException {
S = in.readLine ();
if (S==null) throw new EOFException();
T = new StringTokenizer (S);
}
// Format methods
// --------------
private static DecimalFormat N = new DecimalFormat();
private static final String spaces = " ";
public static String format(double number, int align, int frac) {
N.setGroupingUsed(false);
N.setMaximumFractionDigits(frac);
N.setMinimumFractionDigits(frac);
String num = N.format(number);
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
return num;
}
public static String format(int number, int align) {
N.setGroupingUsed(false);
N.setMaximumFractionDigits(0);
String num = N.format(number);
if (num.length() < align)
num = spaces.substring(0,align-num.length()) + num;
return num;
}
public static String writeDouble (double number, int align, int frac) {
// Deprecated in July 1999 but retained for compatability
return format (number, align, frac);
}
public static String writeInt (int number, int align) {
// Deprecated in July 1999 but retained for compatability
return format (number, align);
}
}
This is the TrainTravel.jar file:
import javagently.*;
class TrainTravel {
/* The Train Travel Program J M and N T Bishop
======================== 1990, 1999
*
* Calculates the distance travelled under acceleration,
* constant speed and deceleration.
* Illustrates constants, variables, expressions and printing
*/
static final double startAcceleration = 0.5; // m/s/s
static final double deceleration = 1.0; // m/s/s
static final double time1 = 2; // min
static final double time2 = 3; // min
static final double kilo = 1000;
// metre to km conversion factor
public static void main (String args []) {
System.out.println("******* Train travel *******\n");
System.out.println(startAcceleration +
" m/s/s decreasing to 0 in " +
time1 + " mins,");
System.out.println("constant speed for " + time2 + " mins,");
System.out.println("and " + deceleration + "m/s/s to rest\n");
// Perform the calculation in simple steps, saving
// each partial result
double secs1 = time1*60; // time1 in secs
double secs2 = time2*60; // time2 in secs
double velocity = 0.5 * startAcceleration * secs1;
double distance1 = (startAcceleration * secs1 * secs1) / 3;
double distance2 = velocity * secs2;
double distance3 = velocity * velocity / (2 * deceleration);
double totalDistance = (distance1 + distance2 + distance3)
/ kilo;
System.out.println
("The journey covers " + Text.format(totalDistance,5,2) + " km");
}
}