-2

How is the java utility that begins the process of launching a class told to "spill its guts" on what it's doing as it tries to load classes?

In particular, what file paths is it TRYING to access, only to perhaps discover whatever it's looking for is not there, at least as it interprets the specification given? There was a way to get that information, but I can't find it now.

Note that this is Java version "1.8.0_333" on Windows 10.

I've tried every flag known to me, via the -h and -X flags, and I strongly suspect what I'm looking for is (was) an X flag that's been removed, just as the -X help output warns. And so, there must be an OS way to figure this out, I sure hope!

You might ask why? Whatever for? What are you trying to do? Well, that's the bulk of this question's text. To wit:

As one of the very early users of Java (I started with 1.1) way back in the '90s, I had an issue moving an application suite I'd written for my company on Linux to MS Windows and I got it working by using Cygwin. Along the way, this same sort of issue came up and I quite vividly recall having found a mechanism for getting the Java launcher to articulate just what file specifications - paths - it was actually using in searching for the appropriate class. And through using this, I found that the CLASSPATH was being specified incorrectly, and with some experimentation, I got it working reliably. Now I need to do that again!

This flag I'd used was immensely helpful in figuring out just what the file specification format CLASSPATH needed to be (we're not talking semicolons here) this combination of OS, Java, and Cygwin. After some hours of what I hope was reasonable hunting, I'm wondering if this capability has been removed at some point? Either that or "I'm looking for the wrong thing." Heck, since the source is available (I think!), maybe some brave soul has hacked the java utility to do such a thing?

It may help to understand that for this application I wrote for my company, it was a major goal to have the source work pretty much the same on all Windows and Linux / Unix systems (and at the time, macOS), and just use a configuration file to tell the code what's different. And that wasn't easy to figure out, but with this flag, it wasn't that hard, either.

But, unfortunately, I haven't needed this knowledge since I figured it all out all those years ago, and apparently, this little kernel of knowledge is very hard to find today. Or, it's no longer pertinent to the modern version(s).

I don't think this has anything much to do with the actual problem, but it may help in people's thinking if they understood the scenario: The current situation is that I have a fully functional installation of this software on Windows 7 to use as a comparison for how to configure things on Windows 10 (and hopefully younger). The Windows 7 is running a pretty modern Cygwin installation and very nearly the most modern Java - just a sub-version away from the new installation from last week on a Windows 10 box. (Everything's bright and shiny new on the new box.)

The required format for CLASSPATH on the nearly identical but fully functional Windows 7 system is:

CLASSPATH="C:/opt/OurInstallationDir/lib"

And that's it.

This value is picked up in several places as the code later needs to launch Java itself to do some unusual things. However, the java command that gets it all going is launched from a C program - not that that matters for this problem - but the C program (compiled under Cygwin, but perfectly runnable from any Windows environment) helps ensure that the Java environment is secured (policy file contents and so forth) before getting into Java, else it refuses. And this program on Windows 11 launches Java just fine, it's just giving it a CLASSPATH that isn't useful, apparently, even though the files are there where they should be, etc.

Configuring things as before just doesn't work, even from the command line. No version of specifying CLASSPATH seems to work if it's more than a dot; the only thing that works, is being in the /lib directory when starting and using "-cp ." ... But that's just not going to fly for so many reasons! To be a little more clear, I've tried reversing the slashes, using /cygdrive/c/, and whatever else I could think of. But, at least we know that if you're in the directory and use -cp, it will find and launch the program. So, there's nothing wrong with the Java, just pointing the java utility at it.

Again: How is the java utility that begins the process of launching a class told to "spill its guts" on what it's doing as it's trying to load classes?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Richard T
  • 4,570
  • 5
  • 37
  • 49
  • Does this `C:/opt/OurInstallationDir/lib` contain bare `.class` files, or JARs? If it contains JARs, you need to specify the individual jars on the classpath, not the directory. – Mark Rotteveel Jun 27 '22 at 07:23
  • 1
    In any case, is this what you're looking for: [How to track when class is loaded and destroyed in jvm?](https://stackoverflow.com/questions/9921081/how-to-track-when-class-is-loaded-and-destroyed-in-jvm) – Mark Rotteveel Jun 27 '22 at 07:24
  • @MarkRotteveel Thanks, Mark; YES, it contains both classes right there in that dir, and several full trees of various packages that may or may not be needed, such as jdbc database access libraries, including one from Oracle. And, they all work fine on other systems. As for your link, "I'm checking it out now," thanks! – Richard T Jun 27 '22 at 17:43
  • @MarkRotteveel YES! I haven't found the configuration problem yet BUT, I have two prospects thanks to you: TraceClassLoading as the primary find and TraceClassLoadingPreorder. as a second sort of maybe. PLEASE create an answer here for this BECAUSE I do not see this as the same question (even though it has this answer under it) and I NEVER would have found it without your help. And I'd like to give you some credit for that! – Richard T Jun 27 '22 at 18:03
  • @MarkRotteveel One I'd originally somehow overlooked AT LEAST confirms what it sees as the CLASSPATH. It's called TraceClassPath. It, in short, echos back the classpath provided, but doesn't say what it thought of it, or tried to do. HOWEVER, unlike the other things I've seen, it DID give back the path formats IT IS USING - perhaps that will lead me somewhere. ...It's using C:\dir\subdir syntax. – Richard T Jun 27 '22 at 22:30
  • @MarkRotteveel I had a flash of insight and solved it. But thanks again! I've saved some notes regarding what you pointed me to - I'm sure it'll be helpful in the future. :-) – Richard T Jun 27 '22 at 22:42

1 Answers1

1

You use this construct on the JVM:

java -XX:+TraceClassPaths -cp "C:\opt\SomeDirectory\lib" myClass

I was able to get confirmation of what Java was using, not only for my CLASSPATH, but "internally" by using the above.

The fact that it echoed back both what I was doing and what it was doing somehow gave me the insight to check everything about it. Java itself doesn't work (at all) if it's installed in a location that it thinks has a link in it, and it's own fetches go right back to the system disk specification.

From that I found that Java on Windows won't take a CLASSPATH that has a link in it!

Simply ensuring that the whole tree was specified "from the top" of the drive it's on works. If it's not, it won't.

It's now working happily using the syntax noted above.

This is quite different from every other application I've seen on Windows. But, well, it's Java!

This really came from a pointer from Mark Rotteveel who commented above about this article: How to track when class is loaded and destroyed in jvm? And therein I learned how to get the list of all the options the presently in-use JVM supports. All Java developers should be aware of this in my opinion, so thanks to Mark for that.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Richard T
  • 4,570
  • 5
  • 37
  • 49