• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiling multiple source files

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just compile the Goo.java then the compiler will compile other java files which are referenced in Goo.java
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Chidimma Juliana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ogeh,

Am sorry I forget to mention that my OS is window vista.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic