• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

basic problem compiling/running code

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all, I feel very stupid, but I can't seem to get "Hello World" dummy program to work. Any help would be appreciated!

I am trying to use the "Head First Java" book to get up to speed with java, but am running into a fundamental problem that is driving me crazy!

Just the facts 'mam:
Running XP, Service Pack 2 (fixes long environmental variable parameter problems)

I downloaded "JDK 5.0 Update 6 with Java EE" from java.sun.com,
the download completed and installed. I decided it was not what I wanted so I uninstalled it using the uninstaller provided.

I then installed the newest JDK: "JDK 5.0 Update 9"
"jdk-1_5_0_09-windows-i586-p-iftw.exe" and it seemed to install without hassles.

Java JDK lives here: d:\jdk-1_5_0_09
Environment variable JAVA_HOME is: d:\jdk-1_5_0_09
Environment variable CLASSPATH is: d:\jdk-1_5_0_09\lib
Environment variable PATH is: PATH=C:\bat;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI.ACE\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\OrCAD\OrCAD_10.0\tools\pcb\bin;C:\OrCAD\OrCAD_10.0\tools\specctra\bin;C:\OrCAD\OrCAD_10.0\tools\PSpice\Library;C:\OrCAD\OrCAD_10.0\too
ls\bin;C:\OrCAD\OrCAD_10.0\tools\fet\bin;C:\OrCAD\OrCAD_10.0\tools\jre\bin;C:\OrCAD\OrCAD_10.0\tools\Capture;d:\jdk-1_5_0_09\bin

When I query javac ("javac -version") I get:
D:\sandbox\project1\src->javac -version
javac 1.5.0_09
javac: no source files ....

When I query java ("java -version") I get:
D:\sandbox\project1\src->java -version
java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b03)
Java HotSpot(TM) Client VM (build 1.5.0_09-b03, mixed mode, sharing


When I execute javac, it compiles without errors:
"javac HelloWorld.java"

and produces a HelloWorld.class file.


Code: (saved as HelloWorld.java)

public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World");
}

}

If I try to run it directly I get the following error:
D:\sandbox\project1\src->java HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java

If I try to execute the HelloWorld class I get a similar error:
D:\sandbox\project1\src->javac HelloWorld.java

D:\sandbox\project1\src->java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

This has to be simple operator error. Any ideas?

Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your CLASSPATH?
 
Fred Freeley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C:\Documents and Settings\David\Desktop>echo %CLASSPATH%
d:\jdk-1_5_0_09\lib;D:\Apache\Tomcat_5_5\common\lib
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The .class file must be found in the CLASSPATH. Since you have a CLASSPATH defined, but it doesn't contain the current directory, java will not see the .class file even if it's in the directory you're currently in.

You can either specify the CLASSPATH when you interpret the program,



or add the current directory to your CLASSPATH

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Keith said works because you are in the directory with the class file and you didn't specify a package in the source file. Read this to learn more.
[ October 22, 2006: Message edited by: Scott Johnson ]
 
Fred Freeley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When I appended a "." to the classpath, I get the following results:

D:\sandbox\project1\src->echo %CLASSPATH%
d:\jdk-1_5_0_09\lib;D:\Apache\Tomcat_5_5\common\lib;.

D:\sandbox\project1\src->type HelloWorld.java

public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World");
}

}
D:\sandbox\project1\src->java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

D:\sandbox\project1\src->java HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java

D:\sandbox\project1\src->javac HelloWorld.java

D:\sandbox\project1\src->java HelloWorld
Hello World

As you can see, if I compile it first, then run it from the *.class, it works. I then tried to force the classpath as you suggested:

D:\sandbox\project1\src->del *.class

D:\sandbox\project1\src->java -cp . HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

D:\sandbox\project1\src->java -cp . HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java

D:\sandbox\project1\src->java -classpath . HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java

D:\sandbox\project1\src->java -classpath . HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

D:\sandbox\project1\src->

I am lead to believe that if a classpath is not specifically defined, the current directory is tried first. I have tried specifying a classpath, forcing a classpath locally and clearing the classpath and forcing the classpath on the command line, with the same result: it will work only if I compile it firat, then execute it from the class. I cannot get it to compile and execute directly such as "java HelloWorld"

I guess it is not a big deal, I can build a batch file that will do that for me, I would just like the command line invocation to work like the book advertizes.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Executing a java application is always a two step process.

You have to compile the .java file to a .class file with javac.

Then you interpret the .class file with java.
 
Fred Freeley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I missed that obvious fact.

Thanks for your patience,

David
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic