• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Difference between wildcard(*.java or source files list) & individual source file compilations.

 
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I have two classes Car and BMW defined in Car.java and BMW.java source files respectively in current working directory say "workspace".

2. Car.java file content looks like
------------------------------------------
package automob;
public abstract class Car {
// members & behaviours
}
------------------------------------------

3. BMW.java file content looks like(this class falls in default package)
------------------------------------------
import automob.Car;
class BMW extends Car {
// BMW specific implementation
}
------------------------------------------

4. CLASSPATH is set to current directory "."

My observation is when I am compiling these two files as

$ javac Car.java BMW.java
(or)
$ javac *.java

compilation succeeded, but when trying to compile BMW.java file compilation fails because Car.class should be in sub-directory(package) "automob".

So, can anybody tell me how the following commands are different?
$ javac *.java // compilation succeeded

$ javac BMW.java // compilation fails

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What folder are you running javac command. If your Car class is in a package and your BMW class is not then when you do
javac *.java
should only compile the one java source in that directory.
Similar when you compile just the BMW class, it "should" able to look up or find its dependent class (eg Car) and compile that too.

Does your file structure looks like:
>BMW.java
>automob
|_Car.java

Try running the "javac BMW.java" command in the folder you have BMW.java
 
Lokesh Lov
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, #K. Tsang

my current working directory is workspace, and these two source files(Car.java and BMW.java) are in this directory only(not placed in appropriate directory structure).
When I issues ls command(I use linux) files are listed as.

BMW class has no package declaration, and Car class belongs to automob package, so it should be placed in automob directory to work. But I observed that when I tried

command to compile, even Car.java is not in automob directory, compilation succeeded.
When I issues "ls" command(I use linux) files are listed as.

I want to know how compiler can resolve Car.class from BMW.class definition(which is not placed in appropriate directory automob).

 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah that's why. Your Car class isn't in the proper folder structure. This is why when you do "javac BMW.java" it failed because it expects the Car.java file in the automob folder which is not there.

You should create a "automob" folder and put the Car file inside. Do the javac command at the BMW file level. This way "javac *.java" or "javac BMW.java" will produce the same result. When compiled, the Car.class should be in the automob folder.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic