0

I have this trivial source code. I build it with JDK 6. The build goes fine with JDK 6 and produces two build artifacts: ./Class_B.class and subdir/Class_A.class.

public class Class_B extends Class_A {} (File ./Class_B.java)
public class Class_A {}                 (File ./subdir/Class_A.java)

Applying javah from JDK 6 passes, but javah from JDK 7+ fails:

$ /usr/lib/jvm/java-6-openjdk-amd64/bin/javah -d out -classpath . Class_B
$ /usr/lib/jvm/java-7-openjdk-amd64/bin/javah -d out -classpath . Class_B
Error: cannot access Class_A
  class file for Class_A not found

BUT! It fails only till the modification time of Class_B.class is newer than of Class_B.java.

-rw-rw-r-- 1 aaa aaa 402 Jun  7 12:00 Class_B.class
-rwxr-xr-x 1 aaa aaa 139 Jun  7 11:56 Class_B.java

If I "touch" the .java then the new javah also passes!

-rw-rw-r-- 1 aaa aaa 402 Jun  7 12:00 Class_B.class
-rwxr-xr-x 1 aaa aaa 139 Jun  7 12:01 Class_B.java
$ /usr/lib/jvm/java-7-openjdk-amd64/bin/javah -d out -classpath . Class_B
$ echo $?
$ 0

On my opinion it makes sense to fail if the source is newer (the binary was not rebuilt). However it behaves vice-versa: it's OK if the source is newer and fails unless -classpath .:subdir is given if the source is older.

WHY??? Where is this change documented? I cannot find in javah documentation (for example: https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javah.html) anything about this modification time check.

UPDATE: Explanation regarding possibility of using "javac -h":

  1. javah in JDK6 works even without -classpath .;./subdir
  2. javah in JDK7 conditionally works without -classpath .;./subdir (depending on the modification time)
  3. javac -h works only with -classpath .;./subdir

I am just interested to know about the difference between "1" and "2". "3" makes no sense, because it can't work without classpath at all.

Alexander Samoylov
  • 2,358
  • 2
  • 25
  • 28
  • Does the same issue happen with more recent Java versions (with `javac -h`)? – dan1st Jun 07 '23 at 10:41
  • It is not possible to check completely the same case with "javac -h". Let me explain please. "javac -h" fails if the "subdir" is not in `-classpath` - YES. The main point is that even without "subdir" in `-classpath` javah works always in JDK6 and _conditionally_ works (depending on the .java modification time) in JDK7,8. In distinct to javah "javac -h" always requires the full classpath to all sources, therefore it is impossible to test my case with "javac -h". – Alexander Samoylov Jun 07 '23 at 11:06
  • What happens with (a recent update version of) `javah` with java 8? – dan1st Jun 07 '23 at 12:16

0 Answers0