aspose file tools*
The moose likes Beginning Java and the fly likes basic problem compiling/running code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic problem compiling/running code" Watch "basic problem compiling/running code" New topic
Author

basic problem compiling/running code

Fred Freeley
Greenhorn

Joined: Sep 24, 2006
Posts: 5
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
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
What is your CLASSPATH?
Fred Freeley
Greenhorn

Joined: Sep 24, 2006
Posts: 5
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

Joined: Feb 07, 2005
Posts: 2367
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

Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
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

Joined: Sep 24, 2006
Posts: 5
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 24, 2006
Posts: 5
I guess I missed that obvious fact.

Thanks for your patience,

David
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic problem compiling/running code