Simple java test

H

Howard Brazee

What is the easiest way someone learning Java can run short little
test programs?

(I don't want to create JBuilder projects with libraries all over the
place for someone who is just wanting to learn)
 
A

Alex Hunsley

Howard said:
What is the easiest way someone learning Java can run short little
test programs?

(I don't want to create JBuilder projects with libraries all over the
place for someone who is just wanting to learn)

This is a good question to ask and for a reason: do it the simply and
straightforwards way. Forget IDEs (as you are indicating you want to
do). If you work with the command line to begin with, you get a feel for
what goes on 'under the hood', what is really happening.

Simplest possible way:
Ensure you have Java installed on your system, and the bin/ directory
for Java is on your path.
open a command prompt. CD into a directory containing a java source file
(e.g. HelloWorld.java).
Compile like so:

javac HelloWorld.java

This creates a HelloWorld.class file.
Run this like so:

java -cp . HelloWorld

(Note: there's no class at the end in above command!)
NB: this assumes your HelloWorld class doesn't have a package statement
in it - i.e. the class is in the 'default' package.
lex
 
O

Oliver Wong

Howard Brazee said:
What is the easiest way someone learning Java can run short little
test programs?

(I don't want to create JBuilder projects with libraries all over the
place for someone who is just wanting to learn)

There's this web game called "Javala" which gives you Java problems for
you to solve. You solve them by typing actual Java code which gets
interpreted on the server side. Presumably, the server side then runs some
unit tests on the provided code to test if you managed to solve the problem.

Anyway, you can just ignore the problem and type whatever code you want,
if you're in a situation where you have access to a web browser, but no JVM
(the website uses JavaScript and the rest is serverside, so you don't even
need the JRE to use it), but need to test some small snippet of Java code.

http://javala.cs.tut.fi/en/top10.do

- Oliver
 
H

Hal Rosser

Howard Brazee said:
What is the easiest way someone learning Java can run short little
test programs?

(I don't want to create JBuilder projects with libraries all over the
place for someone who is just wanting to learn)

A Minimal IDE like "JGrasp" makes the learning experience go smoothly
without getting all bogged down in learning something like Netbeans or
Eclipse. It sort of a text editor that lets you step through your code to
debug it.
Some prefer to work from the command line, but JGrasp is a good compromise.
 
A

Alex Hunsley

Hal said:
A Minimal IDE like "JGrasp" makes the learning experience go smoothly
without getting all bogged down in learning something like Netbeans or
Eclipse. It sort of a text editor that lets you step through your code to
debug it.
Some prefer to work from the command line, but JGrasp is a good compromise.

I think a lot of people recommend trying the command line as a learning
experience (me included) - but I don't really like working with it a
lot, there are tools that are helpful, now that I know what goes on
under the bonnet.
 
H

Howard Brazee

A Minimal IDE like "JGrasp" makes the learning experience go smoothly
without getting all bogged down in learning something like Netbeans or
Eclipse. It sort of a text editor that lets you step through your code to
debug it.
Some prefer to work from the command line, but JGrasp is a good compromise.

That seems like a useful solution. I installed it on my computer and
told it not to change any actions (so JBuilder would continue as it
is).

I tried the applet in chapter two of _Java and JavaScript Programming_
by Peter Wayner, and was able to compile it into the same directory as
the .java file. That chapter said to use a browser to run it. But
it has a System.out.println(message); in it. Firefox says "Applet
Kids started". I would rather not run Resin in this exercise and
don't know if it would help. Is there a different setting that I
need if we are to use this book's examples?

Here's the code:
kids.html:
<HTML>
<HEAD>
<TITLE>Kids Title</TITLE>
</HEAD>
<BODY>
<h1>Kids Header</h1>
<hr>
<applet code="Kids.class" width=200 height=200>
</applet>
</BODY>
</HTML>
=================================
Kids.java:
import java.applet.Applet;

public class Kids extends Applet {
int boredomFactor = 3;
// how long until they quit.
String message = "";
// What they normally say
String quitMessage = "";
// What they say when they quit
public void MyTurn(Kids WhozNext){
if (boredomFactor-- <= 0){
System.out.println(quitMessage);
}
else {
System.out.println(message);
WhozNext.MyTurn(this);
}
}

public void init(){
Kids Bobby, Kenny;
Bobby = new Kids ();
Bobby.message = "Kenny, you did it.";
Bobby.boredomFactor = 4;
Bobby.quitMessage="Fine.";
Kenny = new Kids();
Kenny.message="Bobby, you did it.";
Kenny.quitMessage="Fine";
Kenny.MyTurn(Bobby);
}
// public static void main(String[] args){
// System.out.println("testing Kids");
// }
}
 
D

Daniel Pitts

There's this web game called "Javala" which gives you Java problems for
you to solve. You solve them by typing actual Java code which gets
interpreted on the server side. Presumably, the server side then runs some
unit tests on the provided code to test if you managed to solve the problem.

Anyway, you can just ignore the problem and type whatever code you want,
if you're in a situation where you have access to a web browser, but no JVM
(the website uses JavaScript and the rest is serverside, so you don't even
need the JRE to use it), but need to test some small snippet of Java code.

http://javala.cs.tut.fi/en/top10.do

- Oliver

I had fun with javala.
When I tried it, you couldn't solve the i.o. problems... Unless you
were tricky (like I was)

For a long time, I was the only one with a score above 94.
Now a lot of people have 100. I guess they fixed Dewey,Huey,Louie
(the string tokenizer problem)
 
O

Oliver Wong

Howard Brazee said:
I tried the applet in chapter two of _Java and JavaScript Programming_
by Peter Wayner, and was able to compile it into the same directory as
the .java file. That chapter said to use a browser to run it. But
it has a System.out.println(message); in it. Firefox says "Applet
Kids started". I would rather not run Resin in this exercise and
don't know if it would help. Is there a different setting that I
need if we are to use this book's examples?

Here's the code:
kids.html:
<HTML>
<HEAD>
<TITLE>Kids Title</TITLE>
</HEAD>
<BODY>
<h1>Kids Header</h1>
<hr>
<applet code="Kids.class" width=200 height=200>
</applet>
</BODY>
</HTML>
=================================
Kids.java:
import java.applet.Applet;

public class Kids extends Applet {
int boredomFactor = 3;
// how long until they quit.
String message = "";
// What they normally say
String quitMessage = "";
// What they say when they quit
public void MyTurn(Kids WhozNext){
if (boredomFactor-- <= 0){
System.out.println(quitMessage);
}
else {
System.out.println(message);
WhozNext.MyTurn(this);
}
}

public void init(){
Kids Bobby, Kenny;
Bobby = new Kids ();
Bobby.message = "Kenny, you did it.";
Bobby.boredomFactor = 4;
Bobby.quitMessage="Fine.";
Kenny = new Kids();
Kenny.message="Bobby, you did it.";
Kenny.quitMessage="Fine";
Kenny.MyTurn(Bobby);
}
// public static void main(String[] args){
// System.out.println("testing Kids");
// }
}

I don't know why this program was coded as an applet instead of as an
application. The messages sent via System.out.println() should show up in
the Java console, though.

- Oliver
 
H

Howard Brazee

I found the println output for that applet (without JBuilder nor Resin
running)

It is in C:\Documents and Settings\brazee\Application
Data\Sun\Java\Deployment\log\plugin150_10.trace

I wonder if that can be set to somewhere else.
 
H

Howard Brazee

I don't know why this program was coded as an applet instead of as an
application.

I guess the author was only concerned with web access.
The messages sent via System.out.println() should show up in
the Java console, though.

What Java console? I was running it from a browser. If there's a
setting to redirect it that output, I'd like to know how to do so.

I figured they were in a log file somewhere - my past experience with
log files was that they can be moved around - depending on whether I
used Resin or just JBuilder. So I did a Windows search for files
with my desired output and finally found the log file.

When we do this, I'll have our work directory with a shortcut to that
log directory. The book did not even indicate that the output
wouldn't be immediately visible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top