How to escape whitespaces in directory name in the path for javaagent via DefaultJavaProgramRunner

Faced the issue with adding -javaagent path with whitespaces on Linux and Mac.

Consider a case when the agent JAR file resides at "/home/foo bar/agent.jar" (yes, whith whitspace in the directory name), then adding -javaagent:"/home/foo bar/agent.jar" (escaped with quotes) is fine. This is only if I specify the parameter manually in the Run/Debug configuration window.


The issue is encountered while passing the javaagent parameter in via DefaultJavaProgramRunner in a custom plugin.



To execute the application I've registered a custom runner:

  <extensions defaultExtensionNs="com.intellij">
    <executor implementation="my.runner.MyExecutor"/>
    <programRunner implementation="my.runner.MyRunner"/>
  </extensions>



MyRunner extends DefaultJavaProgramRunner, and mainly its job is to add additional VM parameters to the launch:

    public void patch(JavaParameters javaParameters, RunnerSettings runnerSettings, boolean beforeExecution) throws ExecutionException {
        super.patch(javaParameters, runnerSettings, beforeExecution);
        javaParameters.getVMParametersList().addAt(0, "-javaagent:" + getPathToAgentJar() );  // getPathToAgentJar() returns "/home/foo bar/agent.jar"
    }

So in case of Linux/Mac I could add quotes now (") in order to escape, e.g

  "-javaagent:\"" + getPathToAgentJar() + "\""

This doesn't seem to have any effect however. Here's what is produced in such case if a Java application is executed:

Error occurred during initialization of VM
agent library failed to init: instrument
Error opening zip file or JAR manifest missing : "/home/foo bar/agent.jar"

So it looks like the quotes are considered as if they were a part of the file name.

The same for a server connector:

opt/containers/apache-tomcat-6.0.18/bin/catalina.sh run
Exception in thread "main" java.lang.NoClassDefFoundError: "-javaagent:"/home/foo
Caused by: java.lang.ClassNotFoundException: "-javaagent:".home.foo
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: "-javaagent:"/home/foo.  Program will exit.
Disconnected from server



So in this case it behaves like if there were no quotes at all.

The overall question is, how would it be possible to escape the whitspaces in such case?
10 comments
Comment actions Permalink

This should be fixed in IDEA 11 builds (repository trunk). Could you please check?

0
Comment actions Permalink

Is there a specific nightly build I could test with or it is better to build from sources?
I assume that I should wait for the binary build since server connectors aren't available in the Community Edition.

0
Comment actions Permalink

Is there any way to fix this for IDEA X/10.5 users? Binary-patch a required piece of code at the startup from the plugin itself, or something like that?

The problem hits the new version of JRebel plugin which now includes the agent into distribution and it is installed to "Application Data" directory on Mac by default. So the error is inevitable if the user is on Mac

0
Comment actions Permalink

Sorry, I don't think it's possible - the change was massive.

0
Comment actions Permalink

Tried with the latest EAP today on Ubuntu.

For the standalone Java app:

Error opening zip file or JAR manifest missing : "/home/foo bar/agent.jar"
Error occurred during initialization of VM
agent library failed to init: instrument


Process finished with exit code 1

And for server process:

Exception in thread "main" java.lang.NoClassDefFoundError: "-javaagent:\"/home/foo
Caused by: java.lang.ClassNotFoundException: "-javaagent:\".home.foo
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: "-javaagent:\"/home/foo.  Program will exit.
Disconnected from server



Haven't tried with Mac though..
0
Comment actions Permalink

Could you please post (or attach) full contents of console output, including a command line?

0
Comment actions Permalink

Here's what it looks for a Java app.

/usr/lib/jvm/java-6-sun/bin/java "-javaagent:\"/home/foo bar/agent.jar\"" -Didea.launcher.port=7532 -Didea.launcher.bin.path=/home/jt/downloads/idea-IU-108.1333/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-6-sun/jre/lib/jce.jar:/usr/lib/jvm/java-6-sun/jre/lib/jsse.jar:/usr/lib/jvm/java-6-sun/jre/lib/alt-string.jar:/usr/lib/jvm/java-6-sun/jre/lib/javaws.jar:/usr/lib/jvm/java-6-sun/jre/lib/deploy.jar:/usr/lib/jvm/java-6-sun/jre/lib/plugin.jar:/usr/lib/jvm/java-6-sun/jre/lib/management-agent.jar:/usr/lib/jvm/java-6-sun/jre/lib/resources.jar:/usr/lib/jvm/java-6-sun/jre/lib/rt.jar:/usr/lib/jvm/java-6-sun/jre/lib/alt-rt.jar:/usr/lib/jvm/java-6-sun/jre/lib/charsets.jar:/usr/lib/jvm/java-6-sun/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-6-sun/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-6-sun/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-6-sun/jre/lib/ext/dnsns.jar:/home/jt/downloads/idea-IU-108.1333/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain my.app.Main
Error occurred during initialization of VM
agent library failed to init: instrument
Error opening zip file or JAR manifest missing : "/home/foo bar/agent.jar"


Process finished with exit code 1


And for Tomcat launch:

/opt/containers/apache-tomcat-6.0.18/bin/catalina.sh run
Exception in thread "main" java.lang.NoClassDefFoundError: "-javaagent:\"/home/foo
Caused by: java.lang.ClassNotFoundException: "-javaagent:\".home.foo
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: "-javaagent:\"/home/foo.  Program will exit.
Disconnected from server




This is all what is printed.
0
Comment actions Permalink

The problem here is in a -javaagent:\"/home/foo bar/agent.jar\" part.
Since IDEA 11 you don't need to add quotes around agent path. Just add it as-is, with spaces - IDEA will take care of it when creating a process.

0
Comment actions Permalink

It seems it didn't work out. The unescaped path to the agent is failed to be resolved in the latest EAP (117.84)

Reported the issue to youtrack: http://youtrack.jetbrains.com/issue/IDEA-83614

0

Please sign in to leave a comment.