File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running code

 
corey williams
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
corey williams
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well i have been codeing in notepad but when i compile in my visual studios opens up
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
corey williams
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
corey williams
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks i'll try that
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it compiles
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it's there i get the same thing
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the error "java is not recognized as an internal or external command, operable program or batch file."
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add the bin directory of the JDK to your PATH.
 
Nigel Browne
Ranch Hand
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i do that
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In XP, you can change the enviroment variables by going to Control Panel, then System, then Advanced, then Environment Variables.
 
corey williams
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i still gett the same error when i try getting the "java -version'
 
corey williams
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it to work thank
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EXCELLENT!!!

what was the problem?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic