首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果mvn测试,Janino找不到java.sql.PreparedStatement

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

Stack Overflow用户
提问于 2021-12-14 06:55:26
回答 1查看 185关注 0票数 3

一个简单的单元测试(没有junit)会产生奇怪的异常。

代码语言:javascript
复制
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();
    }
}

信息是:

代码语言:javascript
复制
[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,它将解析如下:

那件事怎么可能?

这是我的pom.xml

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-08 14:59:06

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

正如问题注释中所指出的,这个问题似乎与Maven surefire插件没有使用系统类加载器这一事实有关。请考虑阅读相关文件

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

代码语言:javascript
复制
<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跟踪中所示:

代码语言:javascript
复制
...
[WARNING] useSystemClassloader setting has no effect when not forking
...

您没有使用分叉,这与pom.xml中提供的配置是一致的。

代码语言:javascript
复制
<properties>
    <forkMode>never</forkMode>
</properties>

为了解决这个问题,您可以删除该配置,并且测试将在没有进一步问题的情况下运行。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70344793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档