Please, could someone explain how to compile and run these programs in two different source files.
Below is the error from command prompt when I tried to compile it.
C:\Users\Chidimma>cd C:\Documents and Settings\Chidimma\Desktop\Allfiles
C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac Goo.java
Goo.java:4: package cert does not exist
import cert.*;
^
Goo.java:7: cannot find symbol
symbol : class Sludge
location: class book.Goo
Sludge o = new Sludge();
^
Goo.java:7: cannot find symbol
symbol : class Sludge
location: class book.Goo
Sludge o = new Sludge();
^
3 errors
C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac Goo.java
Like Seetharaman Venkatasamy said, compiling Goo.java will automatically compile other java files referenced in Goo.java. However, you must tell the compiler how to find these other java files if they're not in the current directory. You can use the -classpath or -sourcepath options to do this. If the -sourcepath option is not specified, the classpath is searched for source files as well as class files http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
Let's assume the following:
The path to Goo.java is C:\abc\book\Goo.java
The path to Sludge.java is C:\def\cert\Sludge.java
You can compile Goo.java using any of these commands
Both source files are in one directory. Below are error messages from compiler.
C:\Users\Chidimma>cd C:\Documents and Settings\Chidimma\Desktop\Allfiles
C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac Goo.java
Goo.java:4: package cert does not exist
import cert.*;
^
Goo.java:7: cannot find symbol
symbol : class Sludge
location: class book.Goo
Sludge o = new Sludge();
^
Goo.java:7: cannot find symbol
symbol : class Sludge
location: class book.Goo
Sludge o = new Sludge();
^
3 errors
C:\Documents and Settings\Chidimma\Desktop\Allfiles>javac Goo.java
Chidimma Juliana wrote:Both source files are in one directory
Both source files cannot be in the same folder. Because Goo.java is in the book package, it must be placed inside a book folder. Because Sludge.java is in the cert package, it must be placed inside a cert folder.
I have now created two folders for package book named "book" and another for package cert named "cert" but still unable to compile the program.
Here are the errors.
C:\Users\Chidimma>cd C:\Documents and Settings\Chidimma\Desktop\book
C:\Documents and Settings\Chidimma\Desktop\book>javac -classpath C:\def Goo.java
Goo.java:4: package cert does not exist
import cert.*;
^
Goo.java:7: cannot find symbol
symbol : class Sludge
location: class book.Goo
Sludge o = new Sludge();
^
Goo.java:7: cannot find symbol
symbol : class Sludge
location: class book.Goo
Sludge o = new Sludge();
^
3 errors
when I tried to compile using this path - C:\Documents and Settings\Chidimma\\Desktop\book>javac -sourcepath C:\def Goo.java
The error became,
The filename, directory name or volume syntax is incorrect.
Your -classpath option is wrong. You should use the -classpath option to specify a path that the compiler can use to locate the cert folder. For example, if the cert folder is located in C:\Documents and Settings\Chidimma\Desktop\cert, then you should run the following command
Use the quotation marks or else the compiler will complain about the space in Documents and Settings
On execution side, was not able to execute. Below is the error ;
C:\Documents and Settings\Chidimma\Desktop\book>java -cp . Goo
Exception in thread "main" java.lang.NoClassDefFoundError: Goo (wrong name: book
/Goo)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Goo. Program will exit.
When running the class file, you must use the fully qualified name of the class i.e. book.Goo. You must also provide the classpath to the classes book.Goo and cert.Sludge. Run the following commands:
You can avoid having to manually set the classpath everytime you run the program by setting the CLASSPATH environment variable.
Thank you for your assistance with the classpath, It was successful. Subsequently, I will use code button to paste my programs.
But, how can I permanently set my classpath to avoid doing it manually. I have tried to set it but not working out.
1) Right-click My Computer
2) Click Properties
3) Click the Advanced tab
4) Click Environment variables
5) Under system variables, click New
6) Add a variable name i.e. CLASSPATH
7) Add a variable value i.e. C:\Documents and Settings\Chidimma\Desktop;.
8) Click OK
1) Double click My Computer
2) Click System Properties
3) Click Advanced System Settings
4) Click Environment Variables
5) Under System Variables, click New
6) Add a variable name i.e. CLASSPATH
7) Add a variable value i.e. C:\Documents and Settings\Chidimma\Desktop;.
8) Click OK