This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Hi, all! I'm trying to compile from the command line for the first time. I can compile fine (ie., without an error message), but I don't see any nice HelloWorldApp.class file appearing in my directory.
It sounds like you are using Windows, and you're using the command line, is that right? Not any IDE like Eclipse or IDEA or Netbeans or any of that, I mean. OK, good. You've got an unusually detailed and well-informed problem statement for someone who hasn't been able to compile and run your first Java program. You've apparently eliminated the most common problems encountered at this point, and as a result I can't easily guess what the real problem is. The good news is, your willingness to actually read the instructions and then post clear, detailed questions will prove very valuable skills to you in the long run. If we can just past this initial difficulty.
Can you tell us what prints out when you type "dir" at the command prompt? What directory are you in currently? What command are you using when you compile (or try to)? And what is the output? Please try to answer each question exactly, showing both the commands you used and the results of those commands. It's easiest if you simply copy and paste the results into your post here. Hopefully it's just a matter of selecting that area of your command-line window with a mouse, pressing control-C (copy), then going to your browser's "Post a Reply" page and pasting in the content with a control-V (paste). If that doesn't work, tell us exactly what operating system you're using (XP? Vista? ME? ), and how you're getting to the command line.
Joined: Dec 04, 2008
Hi, Mike! Thanks for the reply. Yes, I neglected to mention that I'm on a Windows machine running Vista. I have actually successfully written, compiled, and run Java programs with JCreator, but decided I wanted to do it from the command line instead. That's when all the trouble started!
At the command line, first I head to the directory where my HelloWorldApp.java file is. That directory is c:\PROGRA~1\Java\jk1.6.0_11. When I then do a directory listing ("dir"), here is what shows up:
Next, I enter "javac HelloWorldApp.java" and hit enter. Things whirr and hum and I get no error message. Then I type the "dir" command again, and find that, alas, the HelloWorldApp.class file that I had so been hoping to see is not there. As far as I can tell, the contents of the directory are precisely the same as before the "javac" command.
Here's a transcript of the entire session:
c:\PROGRA~1\Java\jdk1.6.0_11>dir Volume in drive C is SQ004286V02 Volume Serial Number is CE35-D374
First of all: Do NOT put your .java files in the JDK-instal directory.
Second: How does your HelloWorldApp.java look like? Does it have a package-declaration as the first thing in the file? If it does, the .java file have to be in the same directory structure as the package name.
I.e. if the java file declares package se.lime.demo; you have to put the java-file in the directory <src-root>\se\lime\demo\MyJavaFile.java and compile it, standing in the <src-root> directory with javac -cp . se\lime\demo\MyJavaFile.java. That will put your .class in the same directory as the .java file.
Then you run it with java -cp . se.lime.demo.MyJavaFile, still standing in the <src-root>.
Originally posted by Jessica Hope:  Where would you suggest that I store my .java files? Would the C:\Program Files\Java directory be an appropriate place?  Should I move the HelloWorldApp.java file to a different directory and try compiling a .class file?
 No. Some directory in a "user" area would be better. Suggestion: open your command line, write "mkdir Java" then "cd Java". That will produce a Java folder in "Documents and Settings", but you can put it anywhere that you have access to.
 Yes. Now you have a Java directory, use Windows Explorer or similar to drag'n'drop your HelloWorldApp.java file there, then you should be able to open the command line and write
cd Java javac HelloWorldApp.java java HelloWorldApp
. . . depending on where you put your "Java" folder.
author and iconoclast