Saturday, February 6, 2010

SUBST, Java and Beer ...

I've recently picked up the coding bug again.  I had started going through some C# material and then my girlfriend bought me an Android based phone for Christmas.  Android development is done in Java.  Here was my first experience and a crafty DOS trick that solved my problem.

The "Head First Java" book discourages the use of an IDE while you're learning Java.  The thought is learn the language then learn the tool.  I'm on board so I fired up Notepad++ and entered my code (see below).  I have a shared Dropbox folder setup for Android development.  Nice way to get full versioning and I have it shared with a group of developers for collaboration/help if required.

The book claims their BeerSong.java code will compile and run.  Taking nothing for granted I typed the code in and tried to compile it.  It compiled.  Well, it compiled after I changed "While" to "while."  I've been using M$ stuff too long ;)

Now I had a BeerSong.class file, but it would not run.  My first error was this:

Exception in thread "main" java.lang.NoClassDefFoundError: BeerSong

There were 2 issues.  I made the rookie mistake of trying to execute the code with:

java BeerSong.class

As any old Java salt will be glad to point out, "You don't execute the .class file.  You execute the BeerSong bytecode and it will find the .class file!"  At least it's a common mistake ;)

Mine still did not run.  *sigh*  Turns out I needed a CLASSPATH statement in my environment variables to tell Java where to find my .class files.

That's easy enough, but this is ugly:

set CLASSPATH=C:\Users\bill.mote\Documents\My Dropbox\_AYDABTU.Development\PROJECT.target\BILL\HeadFirstJava\Chapter1

Enter our old friend SUBST.  If you're an old timer you probably remember using that command back in the early 90's to point 1 drive to another.  You can also point a directory to a drive letter!  Try this:

subst b:\ "C:\Users\bill.mote\Documents\My Dropbox\_AYDABTU.Development\PROJECT.target\BILL\HeadFirstJava\Chapter1"

Now you have a drive letter, B, mapped to your path.  Set your CLASSPATH ...

set CLASSPATH=b:\

Now try to run java BeerSong.  Worky!

Just for fun, here's the BeerSong code:

public class BeerSong {
    public static void main (String[] args) {
        int beerNum = 99;
        String word = "bottles";
       
        while (beerNum > 0) {
       
            if (beerNum == 1) {
                word = "bottle"; // singular, as in ONE bottle.
            }
           
            System.out.println(beerNum + " " + word + " of beer on the wall");
            System.out.println(beerNum + " " + word + " of beer.");
            System.out.println("Take one down.");
            System.out.println("Pass it around.");
           
            beerNum = beerNum -1;
           
            if (beerNum > 0) {
                System.out.println(beerNum + " " + word + " of beer on the wall\n");
            } else {
                System.out.println("No more bottles of beer on the wall");
            }
        }
    }
}

 
And, the output.  Notice the grammar problem in the output? :)

No comments:

Post a Comment