Search code examples
javaswingjavaagentsspy

javaagent not working with javaws and JNLP


I have an agent JAR (jar-with-dependencies) with premain as

public static void premain(String args, Instrumentation instrumentation) {
    log.info("Starting Swing Testing Tools");
    log.info("Adding global listener using agent");
    Toolkit.getDefaultToolkit().addAWTEventListener(new GlobalEventListener(), GlobalEventListener.WINDOW_MASK);

    log.info("Starting server");
    Thread serverThread = new Thread(new InterceptorServer());
    serverThread.start();
}

This is an interceptor JAR which will listen for all the events. This class also contains a main method. Purpose of this JAR is to intercept the JNLP so that we can get all the events fired.

Now when i run this JAR using following, the agent is working fine.

D:\temp>java -jar ListenerAgent-1.0.jar
20170716,12.04.49 [INFO ] - (la.core.Agent.premain(Agent.java:16)) Starting Swing Testing Tools
20170716,12.04.49 [INFO ] - (la.core.Agent.premain(Agent.java:17)) Adding global listener using agent
20170716,12.04.49 [INFO ] - (la.core.Agent.premain(Agent.java:20)) Starting server
20170716,12.04.49 [INFO ] - (la.core.server.InterceptorServer.run(InterceptorServer.java:30)) Server started at port : 50173
C:\Users\GAGAND~1\AppData\Local\Temp\

But when i start it using the following cmd line params, the agent doesn't responds but the JNLP opens.

D:\temp>javaws -J-javaagent:"D:\temp\ListenerAgent-1.0.jar" "D:\JNLPs\Notepad.jnlp"
D:\temp>

Here is the Manifest file:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: Gagandeep Singh
Build-Jdk: 1.8.0_111
Specification-Title: ListenerAgent
Specification-Version: 0.0.1-SNAPSHOT
Implementation-Title: ListenerAgent
Implementation-Version: 0.0.1-SNAPSHOT
Implementation-Vendor-Id: org.gagan
Main-Class: la.core.Agent
Can-Redefine-Classes: false
Can-Retransform-Classes: true
Premain-Class: la.core.Agent

Here is the link to my repository in case you need to find any other information.


Solution

  • Posting the solution to this in case someone is facing the same problem.

    You need to modify the javaws.policy inside JRE/bin/security folder of yours:

    • Backup old file
    • Replace the existing text with

      grant { permission java.security.AllPermission; };