Error in java programs
I have a RandomSeq class that prints "args [0]" random doubles and an Average class that prints the average StdIn. I am using DrJava for writing codes and compiling. I downloaded the StdIn and out libraries and put them in the same folder of my classes. I am learning Java.
The first issue is in the Interactions section of DrJava. When I write java RandomSeq 10 > data.txt
instead of creating a text file, it prints the output. Why?
Then I typed the same command at the Windows command prompt. It successfully created the txt file.
Now I want to pipe the output of RandomSeq 10 to the Average tab. Writing java RandomSeq 10 | java Average
in the DrJava Interactions section induces funny behavior. Writing to cmd throws the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: StdIn
at Average.main(Average.java:7)
Caused by: java.lang.ClassNotFoundException: StdIn
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Even java Average < data.txt
shows the same error. Why?
public class RandomSeq
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
for (int i = 0; i < N; i++)
{
System.out.println(Math.random());
}
}
}
public class Average
{
public static void main(String [] args)
{
double S = 0;
int i = 0;
while (!StdIn.isEmpty())
{
double n = StdIn.readDouble();
S += n;
i++;
}
S = S/i;
//StdOut.printf("The mean is %.3f", S);
StdOut.println(S);
}
}
source to share
This is a StdIn related question . StdIn
is not a standard java library, since you mentioned following the Princeton course that is StdIn.java
provided by the course, you probably also need to compile StdIn.java
with the javac StdIn.java
generate file command StdIn.class
, that will solve Class not Found Exception
. (second problem)
As far as the question of what you are writing java RandomSeq 10 > data.txt
in DrJava (which I asked for clarification because I didn't understand DrJava is an IDE, thought it was a book) is that the section Interactions
most likely directly gives input into your program, that is, it passes whatever you type there as args[]
for your function main
.
The output is printed because you are calling system.out.println
into your code and DrJava just takes that output and prints it to the screen and won't translate to your file data.txt
. I would recommend this since you are using course material that requires things like piping to interact with your program on the command line.
Finally java RandomSeq 10 | java Average
- this is the same as when executing java RandomSeq 10
and then taking that output in java Average
, the second command is the same as you do java Average < data.txt
, the error is not because of, but simply because of StdIn
not present in the compiled file .class
...
source to share
StdIn
not from the built-in Java library. It is part of a third party library, which is contained in a file stdlib.jar
that can be downloaded from here .
The path to this file JAR
should be in a parameter -CLASSPATH
on your command line javac
and java
invocations. The Using Standard Libraries section of this page shows you how to do this; parameter -cp
is just shorthand for -CLASSPATH
. It is important to learn how to host libraries on yours CLASSPATH
, as you will be doing this from Java all the time.
Your Average.java
code should also be import
class StdIn
. Since it's StdIn
not in a named package, you just say import StdIn;
at the top of your code.
Alternatively, you can just use System.in
instead of this library. You probably also need a higher level flow construct. Here's a good example .
source to share