Jump to content

External library throws java.lang.reflect.InvocationTargetException but works in DEBUG mode


Jochim

Recommended Posts

I'm working on a bot that automatically reports it's profit to a Google Sheet. I got it working in debug mode with IntelliJ but when I run the compiled jar I get this error.

Failed to start script [Sheets]
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.osbot.t.IIiiIiiiiIII(yr:64)
	at org.osbot.l.IIiiIiiiiIII(hu:233)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NoClassDefFoundError: com/google/auth/Credentials
	at Sheets.<init>(Sheets.java:29)
	... 9 more
Caused by: java.lang.ClassNotFoundException: com.google.auth.Credentials
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 10 more

I'm using maven and following dependencies:

        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.30.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.auth</groupId>
            <artifactId>google-auth-library-oauth2-http</artifactId>
            <version>0.23.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-sheets</artifactId>
            <version>v4-rev20201130-1.31.0</version>
        </dependency>

My build artifact includes all libraries supplied by maven in its root, though I have also tried all other possible combinations with no success (put into output, extract into output, encapsulate in folder/jar).

Has anyone experienced an issue like this in the past and got it resolved? Please let me know!

Edited by Jochim
Link to comment
Share on other sites

  • Jochim changed the title to External library throws java.lang.reflect.InvocationTargetException but works in DEBUG mode

Make sure your project source and output are not in the Scripts directory, and only the built jar is going to be there

Extract all dependencies except for OSBot into output jar

Double check that you have nothing in Scripts directory except for the previously built jar

Start a command prompt

jar tvf <script_jar>

Verify if above contains all dependencies

java -jar <osbot_jar> -autologin -bot email:pass:1234 -script Sheets:-debug 0

If the above fails, read the entire stacktrace from cmd and look for security policy exceptions about reflection

java -jar <osbot_jar> -autologin -allow reflection -bot email:pass:1234 -script Sheets:-debug 0

 

Edited by Token
Link to comment
Share on other sites

 

19 hours ago, Token said:

Make sure your project source and output are not in the Scripts directory, and only the built jar is going to be there

Extract all dependencies except for OSBot into output jar

Double check that you have nothing in Scripts directory except for the previously built jar

Start a command prompt



jar tvf <script_jar>

Verify if above contains all dependencies



java -jar <osbot_jar> -autologin -bot email:pass:1234 -script Sheets:-debug 0

If the above fails, read the entire stacktrace from cmd and look for security policy exceptions about reflection



java -jar <osbot_jar> -autologin -allow reflection -bot email:pass:1234 -script Sheets:-debug 0

 

The project source and output is in a different directory and the only file in the Scripts folder is Sheets.jar.

This are the artifact settings:

image.png.6edc05028b1dc67dcedb2c20eec0495d.png At the bottom is also Sheets compile output.

I ran the following command to make sure the dependency it's complaining about is in the jar.

~/.jdks/adopt-openjdk-1.8.0_282/bin/jar tvf Sheets.jar | grep ServletContextCleaner
  2914 Sun Feb 07 04:14:04 CET 2021 org/apache/commons/logging/impl/ServletContextCleaner.class

Next I ran the following script to launch OSBot with arguments:

#! /bin/bash
~/.jdks/adopt-openjdk-1.8.0_282/bin/java -jar "OSBot.jar" \
        -autologin \
        -allow reflection \
        -bot xxx \
        -script Sheets:null \
        -debug 0 \
        -proxy xxx \
        -login xxx

And it spit out the following rather long error:

Setting reflection permission to allow
Starting local script with name: Sheets
Debug enabled on port 0
[DEBUG][02/11 01:57:20 AM]: Injected 2 field list filters
[DEBUG][02/11 01:57:20 AM]: Injected 2 field filters
[DEBUG][02/11 01:57:20 AM]: Injected 2 method list filters
[DEBUG][02/11 01:57:20 AM]: Injected 3 method filters
In debug mode!
OpenJDK 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
Listening for transport dt_socket at address: 40509
Setup proxy authentication!
Stealth injection class-filter enabled!
Successfully loaded OSBot!
[INFO][02/11 01:57:21 AM]: Welcome to OSBot 2.6.9!
[INFO][02/11 01:57:21 AM]: Loaded 3 RS accounts!
[ERROR][02/11 01:57:21 AM]: Failed to load local script : module-info.class
java.lang.UnsupportedClassVersionError: module-info has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.Lpt2.IiiIiiiiIiii(hu:59)
	at java.lang.Thread.run(Thread.java:748)
[ERROR][02/11 01:57:22 AM]: Failed to load local script : org/apache/commons/logging/impl/ServletContextCleaner.class
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.Lpt2.IiiIiiiiIiii(hu:59)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 16 more
[INFO][02/11 01:57:23 AM]: Script list refreshed and loaded 9 scripts.
[INFO][02/11 01:57:23 AM]: Updated injection hooks for client revision : 193!
[DEBUG][Bot #1][02/11 01:57:23 AM]: Initializing stealth injection bot...
[INFO][Bot #1][02/11 01:57:23 AM]: Downloading latest RS2 old-school client parameters...
[INFO][Bot #1][02/11 01:57:24 AM]: Downloading latest RS2 old-school client...
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected bot reference into client!
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected world accessors!
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected 38 class and 339 field accessors!
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected canvas!
[DEBUG][Bot #1][02/11 01:57:30 AM]: Loading RS world : 31
[INFO][Bot #1][02/11 01:57:30 AM]: Initializing 39 API modules...
[INFO][02/11 01:57:30 AM]: Started bot #1
Waiting for the game to load before starting script...
Starting script...
[ERROR][02/11 01:57:30 AM]: Failed to load local script : module-info.class
java.lang.UnsupportedClassVersionError: module-info has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.BotApplication.main(op:266)
[ERROR][02/11 01:57:31 AM]: Failed to load local script : org/apache/commons/logging/impl/ServletContextCleaner.class
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.BotApplication.main(op:266)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 15 more
Could not find script : Sheets

Regarding this line:

[ERROR][02/11 01:57:22 AM]: Failed to load local script : org/apache/commons/logging/impl/ServletContextCleaner.class

using the jar command above I have confirmed the file is in fact inside the jar.

And regarding these lines:

[ERROR][02/11 01:57:21 AM]: Failed to load local script : module-info.class
java.lang.UnsupportedClassVersionError: module-info has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0

I have no idea why it complains about this because all my IntelliJ settings are set to java 1.8 and the libraries are also confirmed to be working in java 8.

Any further help would be greatly appreciated.

Edited by Jochim
formatting
Link to comment
Share on other sites

It looks like you are compiling your jar with java 9. Try to add this to your pom

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

And compile using maven and see if you still get the same error. If you don't get the same error, then that confirms you compiled using the wrong version. To stick with maven, you may need to use a plugin to include all dependencies though. (Note that the jar won't be in the same directory as when you build directly on intellij unless you set the path)

Edited by Jarl
Link to comment
Share on other sites

11 hours ago, Jochim said:

 

The project source and output is in a different directory and the only file in the Scripts folder is Sheets.jar.

This are the artifact settings:

image.png.6edc05028b1dc67dcedb2c20eec0495d.png At the bottom is also Sheets compile output.

I ran the following command to make sure the dependency it's complaining about is in the jar.


~/.jdks/adopt-openjdk-1.8.0_282/bin/jar tvf Sheets.jar | grep ServletContextCleaner
  2914 Sun Feb 07 04:14:04 CET 2021 org/apache/commons/logging/impl/ServletContextCleaner.class

Next I ran the following script to launch OSBot with arguments:


#! /bin/bash
~/.jdks/adopt-openjdk-1.8.0_282/bin/java -jar "OSBot.jar" \
        -autologin \
        -allow reflection \
        -bot xxx \
        -script Sheets:null \
        -debug 0 \
        -proxy xxx \
        -login xxx

And it spit out the following rather long error:


Setting reflection permission to allow
Starting local script with name: Sheets
Debug enabled on port 0
[DEBUG][02/11 01:57:20 AM]: Injected 2 field list filters
[DEBUG][02/11 01:57:20 AM]: Injected 2 field filters
[DEBUG][02/11 01:57:20 AM]: Injected 2 method list filters
[DEBUG][02/11 01:57:20 AM]: Injected 3 method filters
In debug mode!
OpenJDK 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
Listening for transport dt_socket at address: 40509
Setup proxy authentication!
Stealth injection class-filter enabled!
Successfully loaded OSBot!
[INFO][02/11 01:57:21 AM]: Welcome to OSBot 2.6.9!
[INFO][02/11 01:57:21 AM]: Loaded 3 RS accounts!
[ERROR][02/11 01:57:21 AM]: Failed to load local script : module-info.class
java.lang.UnsupportedClassVersionError: module-info has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.Lpt2.IiiIiiiiIiii(hu:59)
	at java.lang.Thread.run(Thread.java:748)
[ERROR][02/11 01:57:22 AM]: Failed to load local script : org/apache/commons/logging/impl/ServletContextCleaner.class
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.Lpt2.IiiIiiiiIiii(hu:59)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 16 more
[INFO][02/11 01:57:23 AM]: Script list refreshed and loaded 9 scripts.
[INFO][02/11 01:57:23 AM]: Updated injection hooks for client revision : 193!
[DEBUG][Bot #1][02/11 01:57:23 AM]: Initializing stealth injection bot...
[INFO][Bot #1][02/11 01:57:23 AM]: Downloading latest RS2 old-school client parameters...
[INFO][Bot #1][02/11 01:57:24 AM]: Downloading latest RS2 old-school client...
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected bot reference into client!
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected world accessors!
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected 38 class and 339 field accessors!
[DEBUG][Bot #1][02/11 01:57:26 AM]: Injected canvas!
[DEBUG][Bot #1][02/11 01:57:30 AM]: Loading RS world : 31
[INFO][Bot #1][02/11 01:57:30 AM]: Initializing 39 API modules...
[INFO][02/11 01:57:30 AM]: Started bot #1
Waiting for the game to load before starting script...
Starting script...
[ERROR][02/11 01:57:30 AM]: Failed to load local script : module-info.class
java.lang.UnsupportedClassVersionError: module-info has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.BotApplication.main(op:266)
[ERROR][02/11 01:57:31 AM]: Failed to load local script : org/apache/commons/logging/impl/ServletContextCleaner.class
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	at org.osbot.t.IIiiIiiiiIII(yr:43)
	at org.osbot.U.iIiiiiiIIIiI(wx:274)
	at org.osbot.U.IiiIiiiiIiii(wx:259)
	at org.osbot.BotApplication.main(op:266)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 15 more
Could not find script : Sheets

Regarding this line:


[ERROR][02/11 01:57:22 AM]: Failed to load local script : org/apache/commons/logging/impl/ServletContextCleaner.class

using the jar command above I have confirmed the file is in fact inside the jar.

And regarding these lines:


[ERROR][02/11 01:57:21 AM]: Failed to load local script : module-info.class
java.lang.UnsupportedClassVersionError: module-info has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0

I have no idea why it complains about this because all my IntelliJ settings are set to java 1.8 and the libraries are also confirmed to be working in java 8.

Any further help would be greatly appreciated.

Either your script or one of the dependencies is not compatible with java 8 (has been compiled with a newer version); for dependencies you can usually find them in multiple versions, both for java 8 and for newer versions

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...