gpt4 book ai didi

java - 如果 mvn 测试,Janino 找不到 java.sql.PreparedStatement

转载 作者:行者123 更新时间:2023-12-05 03:33:21 25 4
gpt4 key购买 nike

一个简单的单元测试(没有 junit)给出了奇怪的异常

import java.io.StringReader;
import java.sql.PreparedStatement;

import org.codehaus.janino.SimpleCompiler;

public class TestJanino {
public void testJanino() throws Exception {
PreparedStatement ps = null;
String sampleClass = "import java.sql.PreparedStatement; public class Test{}";
SimpleCompiler sc = new SimpleCompiler();
sc.setParentClassLoader(getClass().getClassLoader());
sc.cook("Test.java", new StringReader(sampleClass));
System.out.println(sc.getBytecodes().size());
}

public static void main(String[] args) throws Exception {
TestJanino d = new TestJanino();
d.testJanino();
}
}

消息是:

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xx.xxx.server.nn ---
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO] Surefire report directory: D:\workspace\XXXX\xx.xxx.server.nn\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running xx.xxx.server.nn.TestJanino
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec <<< FAILURE!
xx.xxx.server.nn.TestJanino.testJanino() Time elapsed: 0.056 sec <<< FAILURE!
org.codehaus.commons.compiler.CompileException: File 'Test.java', Line 1, Column 1: A class "java.sql.PreparedStatement" could not be found
at org.codehaus.janino.UnitCompiler.compileError(UnitCompiler.java:12679)
at org.codehaus.janino.UnitCompiler.getSingleTypeImport(UnitCompiler.java:10639)
at org.codehaus.janino.UnitCompiler.checkForConflictWithSingleTypeImport(UnitCompiler.java:454)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:410)
at org.codehaus.janino.UnitCompiler.access$400(UnitCompiler.java:231)
at org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:391)
at org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:386)
at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:1692)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:386)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:359)
at org.codehaus.janino.UnitCompiler.access$000(UnitCompiler.java:231)
at org.codehaus.janino.UnitCompiler$1.visitCompilationUnit(UnitCompiler.java:333)
at org.codehaus.janino.UnitCompiler$1.visitCompilationUnit(UnitCompiler.java:330)
at org.codehaus.janino.Java$CompilationUnit.accept(Java.java:367)
at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:330)
at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:245)
at org.codehaus.janino.SimpleCompiler.compileToClassLoader(SimpleCompiler.java:473)
at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:223)
at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:209)
at xx.xxx.server.nn.TestJanino.testJanino(TestJanino.java:14)


Results :

Failed tests: xx.xxx.server.nn.TestJanino.testJanino(): File 'Test.java', Line 1, Column 1: A class "java.sql.PreparedStatement" could not be found

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.738 s
[INFO] Finished at: 2021-12-14T07:52:08+01:00
[INFO] ------------------------------------------------------------------------

但是如果我在没有 maven 的情况下运行 main 它会解决:

enter image description here

这怎么可能?

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xx.xxx</groupId>
<version>xxx-SNAPSHOT</version>
<properties>
<forkMode>never</forkMode>
</properties>
<artifactId>xxx</artifactId>
<dependencies>
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.6</version>
</dependency>
</dependencies>
</project>

最佳答案

我终于能够使用 Java 11 和 Maven 3.8 重现您的问题。

如问题评论中所示,问题似乎与 Maven surefire 插件未使用系统类加载器这一事实有关。请考虑阅读 relevant documentation .

您可以使用以下插件配置验证这一点:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>

您没有明确提供此配置,但如 Maven 跟踪中所示:

...
[WARNING] useSystemClassloader setting has no effect when not forking
...

您没有使用 fork ,这与您的 pom.xml 中提供的配置一致:

<properties>
<forkMode>never</forkMode>
</properties>

为了解决问题,您可以删除该配置,测试将运行而不会出现其他问题。

话虽如此,正如我在评论中所说,我用不同的 Maven 版本和 JDK 8 测试了代码,它甚至可以将 useSystemClassloader 设置为 false,所以老实说我不能告诉你问题的确切原因,可能与 JDK 版本有关,但也可能是 Maven 版本甚至 Janino。

关于java - 如果 mvn 测试,Janino 找不到 java.sql.PreparedStatement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70344793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com