Search code examples
javaclasspathagent

_JAVA_OPTIONS and CLASSPATH combination


I try to attach an agent to a program.

I use _JAVA_OPTIONS for the following:

export _JAVA_OPTIONS='-javaagent:/java/libs/doeke_sfjl_ui/sfjl_ui_widget_agent.jar'

But it needs a jar file in order to run. I tried solving that by using:

export CLASSPATH=/java/libs/javassist-3.26.0.GA/javassist-3.26.0.GA.jar

But I still get a:

ClassNotFoundException: javassist.expr.ExprEditor

Which lives in javassist-3.26.0.GA.jar.

How can I add javassist-3.26.0.GA.jar before the agent is running?


Solution

  • Various different ways things are going wrong here:

    1. You should avoid these environment variables. The idea behind java is that you may well run 8 different apps at the same time. Especially on windows, environment variables are 'global'; you can specify them per-process especially on unix, but then why aren't you just using the switches by hand? Also, the vast majority of tutorials etc out there presume that your default classpath at the very least includes . (the current dir), which your attempt here overrides, so you're kinda messing up your java install by doing this. Is there a good reason for why you can't just run java -cp whatever -javaagent:thingie.jar?

    2. java agents run in a separate space within the VM, and that space does not get the enjoy the classpath as you specified it. The easiest fix is to have the agent jar fix its own classpath, by shipping with a manifest entry listing its Class-Path. This means you need a file named META-INF/MANIFEST.MF inside the agent jar, and inside is a line of text reading: Boot-Class-Path: lib/a.jar lib/b.jar where [1] space is the separator, [2] forward slashes are used to separate path parts, even on windows, and [3] it is relative to the directory containing this jar. Note, normally you'd use Class-Path: for a java app; Boot-Class-Path is specifically for java agents.

    See section Manifest Attributes in Instrumentation's documentation for more details.