File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes New to Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New to Java" Watch "New to Java" New topic
Author

New to Java

Andrew Casselton
Greenhorn

Joined: Feb 24, 2010
Posts: 6


Hi all,

I'm ploughing through Head First Java, and agree, it is very easy to read and understand (particularly as I'm a grizzled old man from a procedural programming background).

I have JDK 1.6 installed (also NetBeans IDE).

One thing though - the book asks you to solve some code puzzles quite early on - the first PoolPuzzleone for example. I really want to run the code, but the book gives you no indication of how to do this - very frustrating. It shows a kind of console window but doesn't show how to compile a class or run it.
I realise this may be later in the book but I want to do it now - is there a short tutorial on here on achieving that ?

Thanks , Andrew
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

I will assume windows.

Don't use netbeans.

use notepad to write your java file. save it, making sure you don't get the ".txt" extension on the file name.

open a command window, and navigate to where the .java file is.

type "javac ClassName.java"

if that works, type "java ClassName"


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Welcome to the Ranch

Agree with Fred about avoiding NetBeans at this stage, but I would suggest you get something like Notepad++ or Notepad2 or jEdit, which are all much better than MS Notepad for programming. Also have a look at the first few pages of the Java™ Tutorials.
Andrew Casselton
Greenhorn

Joined: Feb 24, 2010
Posts: 6
Thanks guys (already had jEdit)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You're welcome Use jEdit and then compile and run things from the command line. Note some things have .java in the instruction and some things miss out the extension.
javac MyFile.java
. . .
java MyFile
And please note your thread subject doesn't tell us what the question is about.
Andrew Casselton
Greenhorn

Joined: Feb 24, 2010
Posts: 6
Ok so I set the 'Path' as per tutorial on here and ran

javac PoolPuzzleone.java

No errors...so far so good. Then I ran

java PoolPuzzleone

and I got the following message

Exception in thread "main" java.lang.NoClassDefFoundError: PoolPuzzleo
name: PoolPuzzleOne)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(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)
Could not find the main class: PoolPuzzleone. Program will exit.


I then searched a few other threads here and ran following :

java -classpath . PoolPuzzleone

I still get exactly the same error. Appreciate your help (and sorry about the title of the thread - will bear in mind in future).

Thanks in advance

Andrew


Andrew Casselton
Greenhorn

Joined: Feb 24, 2010
Posts: 6
Its ok I was being dumb I didn't realise that the class name was case sensitive - I had one character lower instead of upper case ! Doh !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
And does it work any better with PoolPuzzleOne than PoolPuzzleone?
Andrew Casselton
Greenhorn

Joined: Feb 24, 2010
Posts: 6
Yep it works fine. I came up with a slightly different solution to the one it the book, wanted to test it, and it worked !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New to Java