aspose file tools*
The moose likes Beginning Java and the fly likes Help with running my first program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with running my first program." Watch "Help with running my first program." New topic
Author

Help with running my first program.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Mar 27, 2010
Posts: 23
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.


Thanks and Regards, Saumya Purkayastha - Sam
Specialist - Internet Marketing
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Mar 27, 2010
Posts: 23
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Jun 11, 2007
Posts: 327
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

Joined: Aug 30, 2009
Posts: 217
yes babe


Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Vinod Vijay wrote:yes babe
Really? That makes a very confusing thread even more confusing, I am afraid.
David Book
Greenhorn

Joined: Apr 27, 2010
Posts: 2
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

Joined: Sep 29, 2008
Posts: 12617

Don't add the ".class" extension when you attempt to execute.
David Book
Greenhorn

Joined: Apr 27, 2010
Posts: 2
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 13, 2005
Posts: 39885
    
  28
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.
 
 
subject: Help with running my first program.