Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with running my first program.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A K Kumar wrote:i also have the same problem. i am able to compile my program but could not see it's output. could you please tell me how to see the output of a java program.

Please start a new thread for new questions. When you do, please TellTheDetails, and remember that ItDoesntWorkIsUseless. We have no idea what you're doing, what your program looks like, how you're trying to compile or run it, etc.

And welcome to JavaRanch!
 
Saumya Purkayastha
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Kumar,

Yesterday I was in your situation and I know how much it hurts. Check the notes below which I made for you. If you are able to compile your program, it means you have set your environment variables right. Now its pretty easy to run your program. However I am sending you the complete procedure for compiling and running your program. If you read the entire thread you would have got everything. Anyways I will make it easy for you.

paths:-

JDK for compilation - C:\Program Files\Java\jdk1.6.0_18\bin
Path of file - C:\Users\Sony\Desktop\java
filename - infinite.java


To compile the application.
First command in Command prompt:-
cd C:\Program Files\Java\jdk1.6.0_18\bin

Next command in command prompt(to compile) being in this directory:-
javac C:\Users\Sony\Desktop\java\infinite.java

A .class file gets generated.

To run the file:-
Next command in command prompt:-
cd C:\Users\Sony\Desktop\java

Next command in command prompt(to run) being in this directory:-
java infinite

The above command is java followed by the name of the .class file which is generated.

If your class is a public class the class name and .java file name need to be the same.

If you do not specify public this requirement is ruled out.

Lemme know if you are still facing any problem.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That procedure, I am afraid, is not very good. You appear to be storing your own files in the installation directory. Don't do that; if you uninstall Java and install a new version, all your work might vanish. follow the installation instructions, including §4 about setting the path. Don't set a classpath at this stage.
Create a "java" (or some other name) directory in "my documents" by entering the following at the command line:
mkdir java
cd java
You can create that directory anywhere you wish.
Create and save your .java files with a decent text editor, eg Notepad2, Notepad++, jEdit (I think NotePad is no use for programming).
It tells you in the Java™ Tutorials how to run your very first program.
 
Saumya Purkayastha
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Campbell,

I have not saved my files in the installation directory. I have saved them in a directory called java which I created in the desktop.

Please check the path above. The installation directory is in program files which is required to call the javac compiler.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saumya Purkayastha wrote:Thanks to to all the ppl for replying. . . .
It's not "ppl" please: read this.

And when somebody gives you the correct solution for your problem, please read it and believe it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The cd to program files is incorrect; you should go to the directory you have your .java files in, without going anywhere near Program Files.
 
Harshana Dias
Ranch Hand
Posts: 331
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A K Kumar wrote:i also have the same problem. i am able to compile my program but could not see it's output. could you please tell me how to see the output of a java program.


ranchers are having rest after answering Saumya lol

any way what is your problem? hope you have figure it by your self
 
Vinod Vinu
Ranch Hand
Posts: 217
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes babe
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:yes babe
Really? That makes a very confusing thread even more confusing, I am afraid.
 
David Book
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I'm having two similar problems like this fellow is having. Firstly, I'm using the Head First Java 2nd Edition book to learn java, I think that's what the other fellow is using as well. I run Vista 32 bit and have the JDK version 6. I downloaded and installed Java and the book says to modify and/or add a PATH to javac. Right now I can type c:\java\jdk1.60_20\bin\javac to compile my .java files but i would like to be able to just type javac or the % javac like stated in the book.

I Also cannot run the MyFirstApp.class program from the book.
this is the code the book tells to write:



This is what I get from the C:\ prompt:



I also tried and ran the "HelloWorldApp" from the java website tutorial the code was like this:


The HelloWorldApp file ran just fine when I typed java HelloWorldApp.class into the C:\ prompt. I put the codes side by side and can't find anything different, so I'm not sure what the problem is. Any ideas?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't add the ".class" extension when you attempt to execute.
 
David Book
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok. Great it worked! Thanks. And now just to get the path to javac to work. Do I need to restart my laptop after adding the paths to the system enviroment?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't, but you'd need to open a new command prompt (or add it manually to your current command prompt's path).
 
Campbell Ritchie
Sheriff
Pie
Posts: 47244
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Book: welcome to the Ranch

Please don't add a new question to an old thread; this is regarded as hi-jacking and may deprive the original poster of answers to his own question.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic