*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compiling multiple source files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compiling multiple source files" Watch "Compiling multiple source files" New topic
Author

Compiling multiple source files

Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
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
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

just compile the Goo.java then the compiler will compile other java files which are referenced in Goo.java
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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



Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
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
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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.
Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
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.

Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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


Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Just to be clear. Assuming that these are the paths to the book and cert folders



then you should run the following commands

Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
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.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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.



[Thumbnail for code.jpg]

Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
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.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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
Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
Hello Ogeh,

Am sorry I forget to mention that my OS is window vista.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Chidimma Juliana
Greenhorn

Joined: Jul 14, 2011
Posts: 18
Hello Ogeh,

The Classpath worked for my Vista OS. I am excited about this, thank you so much. You have been helpful.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
You're welcome.
 
Don't get me started about those stupid light bulbs.
 
subject: Compiling multiple source files
 
Similar Threads
Compiling multiple source files.
Command to run when files are in different package
Package problem
doubt in package; compiling
Java package