• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Chapter 1 Exercise

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help figuring out where to type this code and how to execute it.  I have the Zoo.java file  This is what I have so far:


package zoo;

public class Zoo {
   public static void main(String[] args) {
       System.out.println("Welcome!!!");
   }
   
} $javac Zoo.java
$java Zoo


This is what I was instructed to do: To compile and execute this code, type it into a file called Zoo.java and execute the following:
$ javac Zoo.java
$ java Zoo
 
Ranch Hand
Posts: 99
1
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Zoo.java file is wrong:

Line 8 should be "}", and line 9 should be deleted i.e. code should be:

 
Paul Clements
Ranch Hand
Posts: 99
1
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an aside you could use an IDE like Eclipse or NetBeans to create your java code. This would save the need to run manual compiles/runs i.e. java/java
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's more complicated than that because there is a package statement.  I would do this:



The package statement is saying that there is a subdirectory called zoo with a class call Zoo.  

javac zoo/Zoo.java compiles the file "zoo/Zoo.java" in the zoo directory.  

java zoo.Zoo will look for a class file that is in the subdirectory zoo called Zoo.class.
 
Paul Clements
Ranch Hand
Posts: 99
1
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
End of previous post should read:

"This would save the need to execute manual compiles/runs i.e. enter javac/java at the command prompt"
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and Welcome to the Ranch, Dorian Franklin!
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clements wrote:End of previous post should read:

"This would save the need to execute manual compiles/runs i.e. enter javac/java at the command prompt"



I think learning to compile and run (very) basic code from the command line has its advantages.
If nothing else it gives you a good idea of what Java expects the structure of the code (as in files and folders) needs to look like, as well as a better grasp of classpaths than you would get from an IDE.
 
Paul Clements
Ranch Hand
Posts: 99
1
Eclipse IDE MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:I think learning to compile and run (very) basic code from the command line has its advantages.
If nothing else it gives you a good idea of what Java expects the structure of the code (as in files and folders) needs to look like, as well as a better grasp of classpaths than you would get from an IDE.

That's a valid point. For my own purposes I've been happy to use Eclipse. The project explorer gives a good graphical overview of what is where. In addition a quick trip into Windows Explorer confirms what Eclipse shows. That and I can't stand the command prompt window :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic