• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing a main method  RSS feed

 
Jules Vetter
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am following the guide telling you how to write the most basic main method. I am using NetBeans. This is the code in a file called zoo.java



Then it gives the instructions to execute using:
$ javac Zoo.java
$ java Zoo

What does that mean? I've tried typing it in the command line but it doesn't work.
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi welcome to CodeRanch ! 

Since you using NetBeans, why don't you run it by clicking the green button ?

I've tried typing it in the command line but it doesn't work.

Does this shows any error ?
 
Kaustav Pakira
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jules! it seems like you wanna know what those two lines of code does?
javac Zoo.java - used to compile the java class you have written here named Zoo.
java Zoo - used to run the java compiled file to show you the output.
In Netbeans you can see a play button in green in tool bar which will do the work done by these two lines of code!.Else you can right click your mouse anywhere in the blankspace of the Zoo.java terminal you have send the snapshot of and select "run file" option to execute the program.S

Since you have not written any message to be displayed it will show you build succesful which means your program has been executed.
I hope I am able to help you.
 
Jules Vetter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kaustav, thanks so much for replying and explaining those lines.

I ask because another exercise asks to pass parameters, and I don't know how to do that in NetBeans.



It says to use this:
$ javac Zoo.java
$ java Zoo Bronx Zoo


Do you know what I should do?
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could tell us what this guide is? I ask because it seems to be telling you how to compile and run Java code via the command line, but you're using Netbeans instead of the command line. Did it tell you to use Netbeans?
 
Jules Vetter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:
Since you using NetBeans, why don't you run it by clicking the green button ?

I've tried typing it in the command line but it doesn't work.

Does this shows any error ?


Thank you John, it works using the green button, but I asked about the lines because a later exercise uses those commands to pass parameters, and I don't know how to do that in NetBeans (I wrote the example in another comment.)

When I run it in the command line it says '$' is not recognized as an internal or external command.
 
Jules Vetter
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Perhaps you could tell us what this guide is? I ask because it seems to be telling you how to compile and run Java code via the command line, but you're using Netbeans instead of the command line. Did it tell you to use Netbeans?


The guide is Ch. 1, OCA : Oracle Certified Associate Java SE 8 Programmer I: Study Guide Exam 1Z0-808
The course the assigned the reading said to use NetBeans, although the guide itself did not
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the next exercise first, make sure your project can run first


You  will see Hello World in command line if it runs successfully.

But before that, are you supposingly to use command line to run or Netbeans ?
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jules Vetter wrote:When I run it in the command line it says '$' is not recognized as an internal or external command.


Yes, you're not supposed to type the $ character. I suppose that is supposed to mean the prompt which the operating system puts at the command line, which is $ in some cases but can be other things.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, Kaustav Pakira, welcome to the Ranch
 
Nkechi Nnaji
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book "OCA Oracle Certified Associate Java SE 8 Programmer I: Study Guide" wants you to use the command line in the section of the book you are referring to. An IDE like Netbeans or Eclipse will save you time in coding but it hides the details of the java programming language which you need to get a better understanding.

Steps to run your program in command line

1. Ensure you have installed the JDK, the latest being jdk 1.8.0_121. You will find it here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. The default installed location is usually C:\Program Files\Java but check to be sure. You need to know.

2. In a text editor such as notepad, notepad++, enter the code. Save the file and save the file name as filename.java. Note the location of where this file is saved.

3. Open your command prompt and enter the location of filename.java. So if you saved that file in your Documents directory, then in command prompt simply enter cd C:\Users\yourUserName\Documents and press enter. "cd" means change directory.

4. Set the path of the JDK. So enter the following set path=%path%;C:\Program Files\Java\jdk1.8.0_121\bin and press enter. you should not get any errors.

5.Compile the code by typing: javac filename.java and press enter.

6. Run your code by typing: java filename and press enter. Your output will show.

This link will help: http://www.oracle.com/technetwork/java/compile-136656.html

 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nkechi Nnaji wrote: . . . 4. Set the path of the JDK. So enter the following set path=%path%;C:\Program Files\Java\jdk1.8.0_121\bin and press enter. You should not get any errors. . . .
Thank you for all the details, one of the few first posts to earn a cow
But I think there is a potential mistake there in No 4: it is better to do this
set path=C:\Program Files\Java\jdk1.8.0_121\bin%path%;
because you can get problems if there is an older Java® installation anywhere else in the PATH.

And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!