• Post Reply Bookmark Topic Watch Topic
  • New Topic

Chapter 1 Exercise  RSS feed

 
Dorian Franklin
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • 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
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • 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"
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to the Ranch, Dorian Franklin!
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • 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 :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!