wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Running code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running code" Watch "Running code" New topic
Author

Running code

corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
hi, i am new to java and i have written a simple hello code but i can't get it to run i download the java software and the runtime enviroment, and i went through the steps of saving to the java file and trying to run it with the command prompt but when i run it my visual studios opens up and show me my code, at first visual studio showed i had errors i fix ed the error but now all vs does id show the code how can i get it to run
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You need to compile your .java file before you can run the program. Work your way through this tutorial. You should not be writting java in visual studio, use a text editor for the tutorial.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

Work with the excellent Sun tutorial for Getting Started. Once you have the basics going, see if my own Java Environment tips help.

In short, you will need to run a program called "javac" to compile and another called "java" to execute your code. These are both in the "bin" directory of the JDK so that has to be in the Windows path. From the command line it will look like:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
well i have been codeing in notepad but when i compile in my visual studios opens up
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Did you follow the Hello World tutorial to the letter?

What exactly are you clicking on / typing in that causes Visual Studio to appear? You need to give us more details, otherwise we'll just be guessing what you're doing, which will not make it easier to help you.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
well i went downloaded jdk1.6.0 and i saved the file to that file and named it first.java then i wnet to the command prompt a pathed in out to the that folder, because when i tryed to use the javac it said that it could not find it, so once i path it out i do i had c:\program files\java\jdk1.6.0> java.firstand when i hit enter my visual studio open on its own like it was my default for java
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

first, you should set your PATH variable to include the directory where the javac executable is.

it's probably in "c:\program files\java\jdk1.6.0\bin"

once you have that in your path, open a NEW command window. type "javac -version" to make sure it works. if you get "javac is not recognized...", you've done something wrong.

let us know when you get that much working...
[ June 27, 2006: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
thanks i'll try that
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Corey, please follow the Hello World tutorial to the letter.

You haven't done this yet - I know because the errors you get (javac can't be found etc.) are explained in the tutorial, including what you should do if you get that error. You wouldn't be asking those questions if you had carefully followed the tutorial.

So, please, follow the tutorial - it is very easy, and takes you through the whole process in baby steps.
[ June 28, 2006: Message edited by: Jesper Young ]
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
i followed the tutorail, buut now when i try to run the program it tells me that "java is not recognized as interneal or external command
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

can you run the compiler? i.e. when you do the

javac YourFile.java

do you get an OS error, a Java error, or does it compile?
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
it compiles
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

so your os can find javac.exe, but not java.exe?

that's strange... can you check to make sure the java.exe is actually there? it should be in the same spot as your javac.exe...

on my machine, i get this:

C:\Program Files\Java\jdk1.5.0_03\bin>dir java*.exe
Volume in drive C is New Volume
Volume Serial Number is 707B-DB28

Directory of C:\Program Files\Java\jdk1.5.0_03\bin

04/13/2005 03:25 AM 49,248 java.exe
04/13/2005 03:25 AM 49,269 javac.exe
04/13/2005 03:25 AM 49,275 javadoc.exe
04/13/2005 03:25 AM 49,269 javah.exe
04/13/2005 03:25 AM 49,265 javap.exe
04/13/2005 03:25 AM 49,250 javaw.exe
04/13/2005 03:49 AM 127,078 javaws.exe
7 File(s) 422,654 bytes
0 Dir(s) 1,161,285,632 bytes free

C:\Program Files\Java\jdk1.5.0_03\bin>
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
yes, it's there i get the same thing
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

ok... so you're in the directory where you .java file is. you run

"javac YourClass.java"

and it compiles. that means that you now have a YourClass.class file in the directory.

then you say you type

java YourClass

and you get an error message. assuming everything i said is correct, can you post the EXACT text of the error message? if the OS can find javac, it should be able to find java, since you say they are in the same spot...
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
this is the error "java is not recognized as an internal or external command, operable program or batch file."
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You need to add the bin directory of the JDK to your PATH.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
While I don't know which build version of the JDK 1.6 you have, you should be aware that it is a beta version and not a full production version. As you are a beginner and learning the language I recommend that you download the JDK 5.0 update 7 which is the latest full release of the JDK.
As far as helping you run your code, Fred Rosenberger's earlier response which explained how to set the Path variable, coupled with the Java Tutorial should of got you off the ground by now.
When in the C:\Program Files\Java\JDK 1.6.0\bin directory type javac -version on the command prompt and post your output.
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
how do i do that
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In XP, you can change the enviroment variables by going to Control Panel, then System, then Advanced, then Environment Variables.
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
do i add a new path or do i edit the path that is there, because i have trid doing both
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Edit the existing path variable, mine currently looks like this:

When I type java -version at the prompt I get this response:
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

the path variable basically is a list of places for the operating system to look when the executable isn't in the current directory. so you want to add your java directory to the path.

follow Keith's directions to open up the environment variables. select the path one, then at the very end, put in a semi-colon, then the directory, everything from the c: to the final /bin part.

you can then open a dos/command prompt, and type

echo %path%

make sure the command window is opened AFTER you save the change to your path variable. you should see your new directory there.

if so, you should then be able to type "java -version" and get something meaningful.
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
i still gett the same error when i try getting the "java -version'
corey williams
Greenhorn

Joined: Jun 27, 2006
Posts: 26
i got it to work thank
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11172
    
  16

EXCELLENT!!!

what was the problem?
 
jQuery in Action, 2nd edition
 
subject: Running code
 
Similar Threads
Visual Editor Migration From Visual Age to Websphere
Resultset
Visual basic call through java
Database Connectivity
Database Connectivity