Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set up folder and packages? - unable to execute (K&B7)

 
Lin Ahmad
Greenhorn
Posts: 9
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can someone please help with the three questions below? Thanks much in advance!

Book: OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)
Part I OCA and OCP > Declare Class Members > Access Modifiers: Section: Public Members

Below two source files are listed: class Goo in package book, and class Sludge in package cert.

Sludge.java:


Goo.java:


I created a folder C:\TestOtherJavaFiles and then created two subfolders: book and cert.

Created and saved Sludge.java in the cert folder; compiled this class – no issue with this.

Created and saved Goo.java in the book folder - see below;

C:\TestOtherJavaFiles\book

Directory of c:\TestOtherJavaFiles\book

01/01/2016 01:32 PM <DIR> .
01/01/2016 01:32 PM <DIR> ..
01/01/2016 01:23 PM 150 Goo.java
1 File(s) 150 bytes
2 Dir(s) 422,746,611,712 bytes free


Unable to compile the Goo.java in the book folder; the error:

c:\TestOtherJavaFiles\book>javac Goo.java
Goo.java:3: error: package cert does not exist
import cert.*;
^
Goo.java:8: error: cannot find symbol
Sludge o = new Sludge();
^
symbol: class Sludge
location: class Goo
Goo.java:8: error: cannot find symbol
Sludge o = new Sludge();
^
symbol: class Sludge
location: class Goo
3 errors

Ques # 1: It seems that the Goo.java file in the book package is unable to see the other package, cert. As such the javac command should not be run in the book folder, am I right?


I moved back up one folder, to the TestOtherJavaFiles folder, and compiled Goo.java using the command:

c:\TestOtherJavaFiles>javac book\Goo.java. This time it compiled. Below is the class file:

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/01/2016 01:43 PM <DIR> .
01/01/2016 01:43 PM <DIR> ..
01/01/2016 01:43 PM 317 Goo.class
01/01/2016 01:23 PM 150 Goo.java
2 File(s) 467 bytes
2 Dir(s) 422,746,374,144 bytes free


Ques # 2: The compilation worked because at the level of the TestOtherJavaFiles folder, both the cert and the book folders are visible?


Ques # 3:
I tried to execute Goo.class (this has the main method) in the three directories below, none worked. It could not find the main class. Why?


c:\TestOtherJavaFiles\book>java Goo.class
Error: Could not find or load main class Goo.class

c:\TestOtherJavaFiles\book>cd ..

c:\TestOtherJavaFiles>java book\Goo.class
Error: Could not find or load main class book\Goo.class

c:\TestOtherJavaFiles>java Goo.class
Error: Could not find or load main class Goo.class

I did some searches and followed this: http://www.coderanch.com/t/416708/java/java/set-Java-Classpath#1835940

Thanks, appreciate your assistance.

Lina@783
 
Felipe Kunzler
Ranch Hand
Posts: 35
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lin,

Everything is ok, but the last one.

When you are executing a java program, you should always omit the ".class".

So if you run 'java book\Goo' it will work just fine.

Cheers!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lin Ahmad,

First of all, a warm welcome to CodeRanch!

Lin Ahmad wrote:Ques # 1: It seems that the Goo.java file in the book package is unable to see the other package, cert. As such the javac command should not be run in the book folder, am I right?

Lin Ahmad wrote:Ques # 2: The compilation worked because at the level of the TestOtherJavaFiles folder, both the cert and the book folders are visible?

When you execute the javac (and java) command, the current directory is used as the class path (if you don't use the -cp or -classpath option). The class path (as you probably know) is the location where the javac (and java) command has to look for other classes. The Sludge class doesn't have any dependencies on other classes, so you don't need additional classes added to the class path and you can compile this class from both the TestOtherJavaFiles or cert directory. The javac command will be slightly different. From the TestOtherJavaFiles directory you use this javac commandAnd from the cert directory this javac command will compile the Sludge classBoth will successfully compile the Sludge class and create a Sludge.class file in the cert directory.

It's a different story with the Goo class, because this class depends on the Sludge class. So you have to make sure that the dependent classes (in this case the Sludge class) are on the class path when compiling the Goo class (otherwise you'll get an error when compiling as you have experienced). And which directory contains both classes? It's the TestOtherJavaFiles directory. So if you execute the javac command from this directory, the TestOtherJavaFiles directory will be used as current directory and the Sludge class will be found and the Goo class is compiled successfully. And the javac command is very similar to the one to compile the Sludge class from the TestOtherJavaFiles directoryAnd as with the Sludge class, you can also use the javac command from the book directory to compile the Goo class. But it's a bit harder as you need to set the class path explicitly (to include the Sludge class). Because if you execute the javac command from the book directory, the book directory is by default the current directory and the Sludge class won't be found (resulting in an error during compilation as you have experienced). So the javac command from the book directory to compile the Goo class looks likeSo the classpath in this command consists of two directories (on a Windows machine you must use the semicolon as a separator, on a Unix/Linux machine you must use the colon). The first one is . (the dot character) which represents the current directory. If you define a class path (using the -cp or -classpath option), you must explicitly add the current directory (if required). The second one is the relative path .. which denotes the parent directory of the book directory (which is the TestOtherJavaFiles directory in this example). Another alternative was to use an absolute path (instead of the relative path)Because the current directory is not actually required to compile the Goo class from the book directory, it can be removed from the javac commandAll these javac commands will successfully compile the Goo class and create a Goo.class file in the book directory.

Lin Ahmad wrote:Ques # 3: I tried to execute Goo.class (this has the main method) in the three directories below, none worked. It could not find the main class. Why?

That's a much easier explanation. Phew! When you execute the java command you have again to make sure that all required classes are on the class path. So for this example it's the easiest to execute the java command from the TestOtherJavaFiles directory as this directory contains both classes and it will be the current directory. When executing the java command you must use the fully qualified name of the class containing the main method. So in this example, that's book.Goo (as you can see you must not use the .class suffix). So if we combine both suggestions, you can execute the following java command from the TestOtherJavaFiles directory to successfully execute the application (which will print sludge ). But as with the javac command, you can execute the java command from another directory as well, but you need to add the appropriate directory (in this case the TestOtherJavaFiles directory) to the class path. So if you are in the book directory, you'll need to use this java commandAs you can see that's again very similar to the javac command to compile the Goo class from the book directory. Do you want to run the application from the cert directory? The java command will be exactly the same Do you want to execute the application from a random directory (e.g. C:\Windows\System32)? Use the absolute path to the TestOtherJavaFiles directory as the class path

Hope it helps!
Kind regards,
Roel

PS. I was impressed with the quality of your post: a nice overview of all steps you have executed and issues you have encountered, tried to look for an answer to solve your issues, asked clear questions about your doubts/issues and you UseCodeTags. Have a cow for this excellent post!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Felipe Kunzler wrote:When you are executing a java program, you should always omit the ".class".

True!

Felipe Kunzler wrote:So if you run 'java book\Goo' it will work just fine.

That's incorrect! If you use the above command on a Windows machine you'll get this error: Error: Could not find or load main class book\Goo. Although you could use this java command as well (from the TestOtherJavaFiles directory) to execute the application (on a Windows machine)it's considered a best practice to always use the fully qualified name of a class (as it is platform independent and thus will work on a Windows or Linux/Unix or ... machine).
 
Felipe Kunzler
Ranch Hand
Posts: 35
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Felipe Kunzler wrote:So if you run 'java book\Goo' it will work just fine.

That's incorrect! If you use the above command on a Windows machine you'll get this error: Error: Could not find or load main class book\Goo. Although you could use this java command as well (from the TestOtherJavaFiles directory) to execute the application (on a Windows machine)it's considered a best practice to always use the fully qualified name of a class (as it is platform independent and thus will work on a Windows or Linux/Unix or ... machine).


Indeed! When testing I just removed the .class and changed the to a slash (since I was running on bash) and it worked. So I just assumed it would work on Windows cmd with a backslash as well. Turns out that's not true.

Didn't know that, thanks for correcting me Roel!
 
Lin Ahmad
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, thank you very much!

What I needed to understand was classpath (-cp)! I had read about classpath, but somehow just couldn't quite understand it completely. Your explanation makes it very clear.

When you execute the javac (and java) command, the current directory is used as the class path (if you don't use the -cp or -classpath option). The class path (as you probably know) is the location where the javac (and java) command has to look for other classes.


In my case, since it is only in the TestOtherJavaFiles directory that the Sludge class is visible, the javac command has to specify the parent directory in the classpath (..) if I am compiling from the book directory. If I am running the javac command from the TestOtherJavaFiles directory, then I do not need to worry about the classpath and can use this command:
javac book/Goo.java.


Appreciate your explanation of the relative path too. It is very clear.

Below are the successful runs!

# 1:
c:\TestOtherJavaFiles\book>javac -cp .. Goo.java

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/02/2016 08:44 PM <DIR> .
01/02/2016 08:44 PM <DIR> ..
01/02/2016 08:44 PM 317 Goo.class
01/01/2016 02:02 PM 144 Goo.java
2 File(s) 461 bytes
2 Dir(s) 422,754,275,328 bytes free

# 2:
c:\TestOtherJavaFiles\book>javac -cp .;.. Goo.java

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/02/2016 08:42 PM <DIR> .
01/02/2016 08:42 PM <DIR> ..
01/02/2016 08:42 PM 317 Goo.class
01/01/2016 02:02 PM 144 Goo.java
2 File(s) 461 bytes
2 Dir(s) 422,754,275,328 bytes free

# 3:
c:\TestOtherJavaFiles\book>javac -cp c:\TestOtherJavaFiles Goo.java

c:\TestOtherJavaFiles\book>dir
Volume in drive C is OS
Volume Serial Number is F666-5A7F

Directory of c:\TestOtherJavaFiles\book

01/02/2016 08:45 PM <DIR> .
01/02/2016 08:45 PM <DIR> ..
01/02/2016 08:45 PM 317 Goo.class
01/01/2016 02:02 PM 144 Goo.java
2 File(s) 461 bytes
2 Dir(s) 422,754,275,328 bytes free


Roel & Felipe: Also, I was able to execute the Goo class by making sure to use the forward slash - / and not to put the .class at the end of the java command. Thanks!

c:\TestOtherJavaFiles>java book\Goo
Error: Could not find or load main class book\Goo

c:\TestOtherJavaFiles>java book/Goo
sludge

My sincere thanks! Yeah! My first cow

Lin
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lin Ahmad wrote:In my case, since it is only in the TestOtherJavaFiles directory that the Sludge class is visible, the javac command has to specify the parent directory in the classpath (..) if I am compiling from the book directory. If I am running the javac command from the TestOtherJavaFiles directory, then I do not need to worry about the classpath and can use this command:
javac book/Goo.java.

Spot-on!

Finally I like to add for completeness that it's not required to compile the Sludge class before you can compile the Goo class. Assume you have the TestOtherJavaFiles directory with both subdirectories book and cert. In the directory book you only have the source code file Goo.java and the directory cert contains only the source code file Sludge.java.
If the current directory is TestOtherJavaFiles, you can execute this commandThis creates the class file Goo.class containing the Java bytecode for the Goo class. The Goo class uses the Sludge class, and if the file Sludge.class does not already exist (in the appropriate directory, that is cert), the compiler will also compile the source file Sludge.java. Really convenient, isn't it? As you can see, the compiler is a really smart cookie

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic