Forums Register Login

Compiling multiple source files

+Pie Number of slices to send: Send
Hello,

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
+Pie Number of slices to send: Send
just compile the Goo.java then the compiler will compile other java files which are referenced in Goo.java
+Pie Number of slices to send: Send
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



+Pie Number of slices to send: Send
Hello Ogeh,

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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Hello Ogeh,

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.

+Pie Number of slices to send: Send
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


+Pie Number of slices to send: Send
Just to be clear. Assuming that these are the paths to the book and cert folders



then you should run the following commands

+Pie Number of slices to send: Send
Hello Ogeh,

Thank you so much, the program compiled.

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Use the code button to make your code more presentable.

This is the code button. When posting a message, select your code and press the Code button.

code.jpg
[Thumbnail for code.jpg]
+Pie Number of slices to send: Send
Hello Ogeh,

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.
+Pie Number of slices to send: Send
You're welcome.

I'm assuming that Windows XP is your OS

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
+Pie Number of slices to send: Send
Hello Ogeh,

Am sorry I forget to mention that my OS is window vista.
+Pie Number of slices to send: Send
In that case

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

+Pie Number of slices to send: Send
Hello Ogeh,

The Classpath worked for my Vista OS. I am excited about this, thank you so much. You have been helpful.
+Pie Number of slices to send: Send
You're welcome.
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1860 times.
Similar Threads
Compiling multiple source files.
doubt in package; compiling
Package problem
Java package
Command to run when files are in different package
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:35:09.