• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

javac and packages

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something I cannot understand about javac and pacakages. javac is supposed to automatically recompile source files that have recently been modified.
These two source files (Test1.java and Test2.java) are placed in the same folder c:\j2\src.
package p1;
public class Test1{
public static void main(String[] arg){
System.out.println("j: " + p2.Test2.j);
}
}
package p2;
public class Test2{
public static int j = 1;
}
I want to keep the source files and bytecode separated, so I create another directory c:\j2\lib, and compile the two files:
javac -d c:\j2\lib Test2.java
javac -d c:\j2\lib Test1.java
This works fine, the class-files are generated OK. The problem is, if now modify the source file Test2.java (just set j=2), and then compile Test1 again:
javac -verbose -d c:\j2\lib Test1.java
the class Test2 IS NOT recompiled. java p1.Test1 still prints 1. I thought javac were supposed to locate Test2.java, and recompile it since it has been modified.
I run the javac and java tool from the c:\j2\src directory. The classpath settings include . and c:\j2\bin.
I have tried different things with the classpath, and directory structure, but cannot seem to figure it out. Any help is much appreciated.
Regards,
Sven
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sven Thor:

These two source files (Test1.java and Test2.java) are placed in the same folder c:\j2\src.
package p1;
public class Test1{
}
package p2;
public class Test2{
}


The java compiler uses the classpath to look for source files, so the same rules apply. The directory c:\j2\src must be in the classpath and the compiler will expect to find source for package p1 in c:\j2\src\p1 and source for package p2 in c:\j2\src\p2.
 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try compiling *.java instead of each one individually
 
Sven Thor
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help. It worked after including the directory c:\j2\src in the classpath and placing the source for package p1 in c:\j2\src\p1 and source for package p2 in c:\j2\src\p2, like suggested.
br,
Sven
 
crispy bacon. crispy tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic