Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TIJ bruceeckel.simpletest question

 
Mike Ferrando
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,
I have begun to work through Bruce Eckel's excellent book TIJ 3rd edition.

I am rather stumped at the error messages I am getting.

I am trying to successfully compile and run the IfElse.java file.

I am familiar running applications from the command line and I have programming experience.

I have been to a number of sites concerning setup of my java, but I have not found the problem yet.

Can someone tell me what I am doing wrong?

Thanks,
Mike Ferrando
Washington, DC

My PC:
Windows 2000

File: IfElse.java
//: c03:IfElse.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// http://www.cs.hut.fi/Docs/Eckel/TIJ3ed/code/com/bruceeckel/simpletest/

import com.bruceeckel.simpletest.*;

public class IfElse {
static Test monitor = new Test();
static int test(int testval, int target) {
int result = 0;
if(testval > target)
result = +1;
else if(testval < target)
result = -1;
else
result = 0; // Match
return result;
}
public static void main(String[] args) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
monitor.expect(new String[] {
"1",
"-1",
"0"
});
}
} ///:~


C:\z_temp>javac -g -verbose -cp c:\z_temp IfElse.java
[parsing started IfElse.java]
[parsing completed 93ms]
[search path for source files: [c:\z_temp]]
[search path for class files: [C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar,
C:\Program Files\Java\jdk1.5.0_06\jre\lib\jsse.jar, C:\Program Files\Java\jdk1.
5.0_06\jre\lib\jce.jar, C:\Program Files\Java\jdk1.5.0_06\jre\lib\charsets.jar,
C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\dnsns.jar, C:\Program Files\Java\j
dk1.5.0_06\jre\lib\ext\localedata.jar, C:\Program Files\Java\jdk1.5.0_06\jre\lib
\ext\sunjce_provider.jar, C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\sunpkcs1
1.jar, c:\z_temp]]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/Object.class
)]
[loading c:\z_temp\com\bruceeckel\simpletest\Test.class]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/String.class
)]
[checking IfElse]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/System.class
)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/io/PrintStream.cl
ass)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/io/FilterOutputSt
ream.class)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/io/OutputStream.c
lass)]
[wrote IfElse.class]
[total 2843ms]

C:\z_temp>java -cp c:\z_temp IfElse
1
-1
0
Exception in thread "main" java.lang.NoClassDefFoundError: com/bruceeckel/simple
test/OutputVerifier
at com.bruceeckel.simpletest.Test.expect(test.java:77)
at com.bruceeckel.simpletest.Test.expect(test.java:82)
at IfElse.main(IfElse.java:24)

C:\z_temp>
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you have his simpletest jar file?
 
Mike Ferrando
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
do you have his simpletest jar file?



Fred R.,
I did a treeview of the unzipped code from his file and there are no jar files.

Treeview of files below.

I did compile the files with a javac command into class files. I thought that was good enough. Do I need to create a jar(s)?

Mike Ferrando
Washington, DC

+---com
| | build.xml
| |
| \---bruceeckel
| +---simple
| | List.class
| | List.java
| | Vector.class
| | Vector.java
| |
| +---simpletest
| | LineMismatchException.class
| | LineMismatchException.java
| | NumOfLinesException.class
| | NumOfLinesException.java
| | OutputVerifier.java
| | SimpleTestException.class
| | SimpleTestException.java
| | Test.class
| | Test.java
| | TestExpression.class
| | TestExpression.java
| | TestStream$1.class
| | TestStream.class
| | TestStream.java
| |
| +---swing
| | Console.class
| | Console.java
| |
| +---tools
| | | Assert.class
| | | Assert.java
| | | CheckVersion.class
| | | CheckVersion.java
| | | P.class
| | | P.java
| | |
| | \---debug
| | Assert.java
| |
| \---util
| AlphabeticComparator.class
| AlphabeticComparator.java
| Arrays2$RandBooleanGenerator.class
| Arrays2$RandByteGenerator.class
| Arrays2$RandCharGenerator.class
| Arrays2$RandDoubleGenerator.class
| Arrays2$RandFloatGenerator.class
| Arrays2$RandIntGenerator.class
| Arrays2$RandLongGenerator.class
| Arrays2$RandShortGenerator.class
| Arrays2$RandStringGenerator.class
| Arrays2.class
| Arrays2.java
| BooleanGenerator.class
| BooleanGenerator.java
| ByteGenerator.class
| ByteGenerator.java
| CharGenerator.class
| CharGenerator.java
| Collections2$RandStringPairGenerator.class
| Collections2$StringGenerator.class
| Collections2$StringPairGenerator.class
| Collections2.class
| Collections2.java
| CountryCapitals.class
| CountryCapitals.java
| DoubleGenerator.class
| DoubleGenerator.java
| FloatGenerator.class
| FloatGenerator.java
| Generator.class
| Generator.java
| IntGenerator.class
| IntGenerator.java
| LongGenerator.class
| LongGenerator.java
| MapGenerator.class
| MapGenerator.java
| Pair.class
| Pair.java
| ShortGenerator.class
| ShortGenerator.java
| TextFile.class
| TextFile.java
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my bad... i just assumed he gave you a jar file... no, you do not need to make one.

basically, the compiler is telling you that it doesn't know where to find the OutputVerifier class. from your tree, i don't see that that one compiled. you have the class files for everything else.

are you sure you didn't get an error message when you compiled his code? it just seems strange that that one didn't show up.

i've not downloaded his code and compiled it, so i'm not sure i can be of much more help. have you checked his web site for errata? i've also never used Ant (i assume that's what he's using here). can you look in the build.xml file and see if OutputVerifier is listed like the others in that group?
 
Mike Ferrando
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
my bad... i just assumed he gave you a jar file... no, you do not need to make one.

basically, the compiler is telling you that it doesn't know where to find the OutputVerifier class. from your tree, i don't see that that one compiled. you have the class files for everything else.

are you sure you didn't get an error message when you compiled his code? it just seems strange that that one didn't show up.

i've not downloaded his code and compiled it, so i'm not sure i can be of much more help. have you checked his web site for errata? i've also never used Ant (i assume that's what he's using here). can you look in the build.xml file and see if OutputVerifier is listed like the others in that group?


Fred R.,
Ok, I see it. Yep, it did not compile.

I tried to compile it and received an error. (below)

I guess I can tick around with it and figure it out, etc.

If it seems obvious to you let me know so I can correct it.

I will check the errata meanwhile.

Thanks,
Mike Ferrando
Washington, DC

OutputVerifier.java
//: com:bruceeckel:simpletest utputVerifier.java
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package com.bruceeckel.simpletest;
import java.util.*;
import java.io.PrintStream;

public class OutputVerifier {
private static void verifyLength(
int output, int expected, int compare) {
if((compare == Test.EXACT && expected != output)
|| (compare == Test.AT_LEAST && output < expected))
throw new NumOfLinesException(expected, output);
}
public static void verify(List output, List expected) {
verifyLength(output.size(),expected.size(),Test.EXACT);
if(!expected.equals(output)) {
//find the line of mismatch
ListIterator it1 = expected.listIterator();
ListIterator it2 = output.listIterator();
while(it1.hasNext()
&& it2.hasNext()
&& it1.next().equals(it2.next()));
throw new LineMismatchException(
it1.nextIndex(), it1.previous().toString(),
it2.previous().toString());
}
}
public static void
verifyIgnoreOrder(List output, Object[] expected) {
verifyLength(expected.length,output.size(),Test.EXACT);
if(!(expected instanceof String[]))
throw new RuntimeException(
"IGNORE_ORDER only works with String objects");
String[] out = new String[output.size()];
Iterator it = output.iterator();
for(int i = 0; i < out.length; i++)
out[i] = it.next().toString();
Arrays.sort(out);
Arrays.sort(expected);
int i =0;
if(!Arrays.equals(expected, out)) {
while(expected[i].equals(out[i])) {i++;}
throw new SimpleTestException(
((String) out[i]).compareTo(expected[i]) < 0
? "output: <" + out[i] + ">"
: "expected: <" + expected[i] + ">");
}
}
public static void
verifyAtLeast(List output, List expected) {
verifyLength(output.size(), expected.size(),
Test.AT_LEAST);
if(!output.containsAll(expected)) {
ListIterator it = expected.listIterator();
while(output.contains(it.next())) {}
throw new SimpleTestException(
"expected: <" + it.previous().toString() + ">");
}
}
} ///:~



C:\z_temp>cmd
Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\z_temp>javac -g -verbose com\bruceeckel\simpletest\OutputVerifier.java
[parsing started com\bruceeckel\simpletest\OutputVerifier.java]
[parsing completed 156ms]
[search path for source files: [.]]
[search path for class files: [C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar,
C:\Program Files\Java\jdk1.5.0_06\jre\lib\jsse.jar, C:\Program Files\Java\jdk1.
5.0_06\jre\lib\jce.jar, C:\Program Files\Java\jdk1.5.0_06\jre\lib\charsets.jar,
C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\dnsns.jar, C:\Program Files\Java\j
dk1.5.0_06\jre\lib\ext\localedata.jar, C:\Program Files\Java\jdk1.5.0_06\jre\lib
\ext\sunjce_provider.jar, C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext\sunpkcs1
1.jar, .]]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/io/PrintStream.cl
ass)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/Object.class
)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/util/List.class)]

[checking com.bruceeckel.simpletest.OutputVerifier]
[loading .\com\bruceeckel\simpletest\Test.class]
[loading .\com\bruceeckel\simpletest\NumOfLinesException.class]
[loading .\com\bruceeckel\simpletest\SimpleTestException.class]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/String.class
)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/RuntimeExcep
tion.class)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/Throwable.cl
ass)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/Exception.cl
ass)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/util/Collection.c
lass)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/lang/Iterable.cla
ss)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/util/ListIterator
.class)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/util/Iterator.cla
ss)]
[loading .\com\bruceeckel\simpletest\LineMismatchException.class]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/util/Arrays.class
)]
[loading C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar(java/util/Comparator.c
lass)]
com\bruceeckel\simpletest\OutputVerifier.java:45: compareTo(java.lang.String) in
java.lang.String cannot be applied to (java.lang.Object)
((String) out[i]).compareTo(expected[i]) < 0
^
[total 3250ms]
Note: com\bruceeckel\simpletest\OutputVerifier.java uses unchecked or unsafe ope
rations.
Note: Recompile with -Xlint:unchecked for details.
1 error

C:\z_temp>
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Ferrando:
...com\bruceeckel\simpletest\OutputVerifier.java:45: compareTo(java.lang.String) in
java.lang.String cannot be applied to (java.lang.Object)
((String) out[i]).compareTo(expected[i]) < 0
^
[total 3250ms]
Note: com\bruceeckel\simpletest\OutputVerifier.java uses unchecked or unsafe ope
rations.
Note: Recompile with -Xlint:unchecked for details.
1 error...

On line 45, try calling toString() on the Object referenced by expected[i].

Note that Eckel's 3rd edition was prior to Java 5.0 (which introduced generics), so when using his examples with a 5.0 compiler, you can ignore the warnings "...uses unchecked or unsafe operations." and "...Recompile with -Xlint:unchecked..."
 
Mike Ferrando
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred,
That did it!

Thanks much,
Mike Ferrando
Washington, DC
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, but i think it was marc who had the final solution.
 
kay sam
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!
I thank all the guys who make this wonderful site.
I love to be here.

Please do bear with me(coz im a newbie) for reopening the above topic.
Please correct me. Even after adding toString() , runtime Exception does occur for IfElse.java

As marc weber said I modified the 45th line in com\bruceeckel\simpletest\OutputVerifier.java file

((String) out[i]).compareTo(expected[i]) < 0
as
((String) out[i]).compareTo(expected[i].toString()) < 0


Could compile OutputVerifier.java file successfully without exception.
I can compile the Ifelse.java file too without any errors.

But runtime exception does occur:



Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic