I have a problem with trying to make a shell in groovy. I am using groovy 3.0.9 and am compiling it with my jar.
The code in question:
public class GrPlayerShell extends PlayerShell {
private final Groovysh groovysh;
private final PrintStream out;
private final PrintStream err;
protected GrPlayerShell(UUID uuid) {
super(uuid, GrShell.getInstance());
out = new PrintStream(new PlayerStream(uuid, false));
err = new PrintStream(new PlayerStream(uuid, true));
this.groovysh = new Groovysh(new IO(
new InputStream() {
@Override
public int read() throws IOException {
throw new IOException("Not implemented");
}
},
out,
err));
}
@Override
public void doExecute(String command) {
execute(command);
}
@Override
public Result execute(String command) {
if (command == null || command.isEmpty()) {
return Result.EMPTY;
}
Object result = groovysh.execute(command); // FIXME: 12/26/21 NoClassDefFoundError: groovy.lang.Script
return new Result(String.valueOf(result));
}
}
(also here)
The doExecute
and execute
are running in a different thread to prevent the shell from pausing everything if the shell takes a while to execute (for example a while (true) {}
).
This jar also gets loaded dynamically in runtime.
I have tested with java 8 and 17, yet both give exactly the same result.
Just encase the error message is of any importance:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during instruction selection: java.lang.NoClassDefFoundError: groovy/lang/Script
java.lang.RuntimeException: java.lang.NoClassDefFoundError: groovy/lang/Script
at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:976)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:671)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:635)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:389)
at groovy.lang.GroovyClassLoader.lambda$parseClass$3(GroovyClassLoader.java:332)
at org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163)
at org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:330)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:526)
at groovy.lang.GroovyShell.parse(GroovyShell.java:538)
at groovy.lang.GroovyShell.parse(GroovyShell.java:570)
at groovy.lang.GroovyShell$parse$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
at org.apache.groovy.groovysh.Interpreter.evaluate(Interpreter.groovy:76)
at org.apache.groovy.groovysh.Evaluator$evaluate.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
at org.apache.groovy.groovysh.Groovysh.execute(Groovysh.groovy:204)
at uwu.smsgamer.serverscripter.groovy.shell.GrPlayerShell.execute(GrPlayerShell.java:67)
at uwu.smsgamer.serverscripter.groovy.shell.GrPlayerShell.doExecute(GrPlayerShell.java:58)
at uwu.smsgamer.serverscripter.shell.PlayerShell$1.run(PlayerShell.java:86)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.NoClassDefFoundError: groovy/lang/Script
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at groovy.lang.GroovyClassLoader.access$400(GroovyClassLoader.java:90)
at groovy.lang.GroovyClassLoader$ClassCollector.createClass(GroovyClassLoader.java:700)
at groovy.lang.GroovyClassLoader$ClassCollector.onClassNode(GroovyClassLoader.java:717)
at groovy.lang.GroovyClassLoader$ClassCollector.call(GroovyClassLoader.java:721)
at org.codehaus.groovy.control.CompilationUnit$3.lambda$call$0(CompilationUnit.java:806)
at java.util.Optional.ifPresent(Optional.java:159)
at org.codehaus.groovy.control.CompilationUnit$3.call(CompilationUnit.java:806)
at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:942)
... 25 more
Caused by: java.lang.ClassNotFoundException: groovy.lang.Script
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:869)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:979)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:967)
at groovy.lang.GroovyClassLoader$InnerLoader.loadClass(GroovyClassLoader.java:607)
... 36 more
1 error
[09:49:47 WARN]: at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:295)
[09:49:47 WARN]: at org.codehaus.groovy.control.ErrorCollector.addException(ErrorCollector.java:143)
[09:49:47 WARN]: at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:976)
[09:49:47 WARN]: at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:671)
[09:49:47 WARN]: at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:635)
[09:49:47 WARN]: at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:389)
[09:49:47 WARN]: at groovy.lang.GroovyClassLoader.lambda$parseClass$3(GroovyClassLoader.java:332)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154)
[09:49:47 WARN]: at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:330)
[09:49:47 WARN]: at groovy.lang.GroovyShell.parseClass(GroovyShell.java:526)
[09:49:47 WARN]: at groovy.lang.GroovyShell.parse(GroovyShell.java:538)
[09:49:47 WARN]: at groovy.lang.GroovyShell.parse(GroovyShell.java:570)
[09:49:47 WARN]: at groovy.lang.GroovyShell$parse$0.call(Unknown Source)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
[09:49:47 WARN]: at org.apache.groovy.groovysh.Interpreter.evaluate(Interpreter.groovy:76)
[09:49:47 WARN]: at org.apache.groovy.groovysh.Evaluator$evaluate.call(Unknown Source)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
[09:49:47 WARN]: at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
[09:49:47 WARN]: at org.apache.groovy.groovysh.Groovysh.execute(Groovysh.groovy:204)
[09:49:47 WARN]: at uwu.smsgamer.serverscripter.groovy.shell.GrPlayerShell.execute(GrPlayerShell.java:67)
[09:49:47 WARN]: at uwu.smsgamer.serverscripter.groovy.shell.GrPlayerShell.doExecute(GrPlayerShell.java:58)
[09:49:47 WARN]: at uwu.smsgamer.serverscripter.shell.PlayerShell$1.run(PlayerShell.java:86)
[09:49:47 WARN]: at java.util.TimerThread.mainLoop(Timer.java:555)
[09:49:47 WARN]: at java.util.TimerThread.run(Timer.java:505)
I have no idea why this might be happening because I put a Class.forName("groovy.lang.Script")
before and even made my own class that extends that script class and they both work just fine. Why groovy is complaining that its own script class don't exist, I have 0 idea why.
Pliz help
CodePudding user response:
not an answer. just a guess.
if you are not specifying class loader for groovysh by default it takes Thread.currentThread().contextClassLoader
so, at the moment of execution currentThread().contextClassLoader
differs from classloader where it's successful for your test Class.forName("groovy.lang.Script")
check that groovy libraries accessible for thread context classloader or change classloader when creating groovish object.
check that you have all groovy dependencies for runtime