Gradle:如何编译使用 com.sun.xml.internal.bind.* 的类?

发布于 2024-12-10 02:22:37 字数 3093 浏览 0 评论 0原文

我正在将现有项目从 ant 转换为 Gradle。

不幸的是,有一个 Java 类使用 com.sun.xml.internal.bind.DatatypeConverterImpl。一方面,我将考虑替换该用法,但此时我只是好奇为什么 Gradle 找不到该类,从而导致编译失败。

消息是

package com.sun.xml.internal.bind does not exist

注意它在 eclipse 中编译良好以及使用 javac ant 任务。 JAVA_HOME 设置为 1.6.0_27。

那么,Gradle 的独特之处是什么?默认情况下会导致它找不到此类,以及如何解决这个问题?




请参阅 Java 代码片段,后跟 build.gradle 文件:

final class Test{
    private static final javax.xml.bind.DatatypeConverterInterface DTC = com.sun.xml.internal.bind.DatatypeConverterImpl.theInstance;

    .....
    DTC.printDateTime(Calendar.getInstance());
}


apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.foo.bar.Test"
sourceCompatibility = 1.6
version = '5.4.0'

jar {
    manifest {
        attributes 'Implementation-Title': 'Foo', 'Implementation-Version': version, 'Implementation-Vendor': 'Bar'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-lang', name: 'commons-lang', version: '2.4'    
    compile group: 'com.google.guava', name: 'guava', version: '10.0.1'    
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.1.Final'    
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    compile group: 'jdom', name: 'jdom', version: '1.0'        
    compile group: 'log4j', name: 'log4j', version: '1.2.16'

    runtime group: 'antlr', name: 'antlr', version: '2.7.6'
    runtime group: 'backport-util-concurrent', name: 'backport-util-concurrent', version: '3.1'
    runtime group: 'c3p0', name: 'c3p0', version: '0.9.1.2'
    runtime group: 'cglib', name: 'cglib', version: '2.2'
    runtime group: 'commons-beanutils', name: 'commons-beanutils', version: '1.8.3'
    runtime group: 'commons-codec', name: 'commons-codec', version: '1.3'
    runtime group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
    runtime group: 'commons-io', name: 'commons-io', version: '1.4'    
    runtime group: 'commons-logging', name: 'commons-logging', version: '1.1.1'    
    runtime group: 'dom4j', name: 'dom4j', version: '1.6.1'
    runtime group: 'javassist', name: 'javassist', version: '3.12.0.GA'    
    runtime group: 'javax.transaction', name: 'jta', version: '1.1'        
    runtime group: 'org.slf4j', name: 'slf4j-api', version: '1.6.2'
    runtime group: 'xerces', name: 'xerces', version: '2.4.0'        
    runtime group: 'xerces', name: 'xercesImpl', version: '2.10.0'
    runtime group: 'xml-apis', name: 'xml-apis', version: '2.0.2'                
}

编辑

C:\>gradle -v

------------------------------------------------------------
Gradle 1.0-milestone-3
------------------------------------------------------------

Gradle build time: Monday, 25 April 2011 5:40:11 PM EST
Groovy: 1.7.10
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.6.0_27 (Sun Microsystems Inc. 20.2-b06)
OS: Windows XP 5.1 x86

I'm in the process of converting an existing project from ant to Gradle.

Unfortunately there is a Java class that uses com.sun.xml.internal.bind.DatatypeConverterImpl. For one thing I'm going to look into replacing that usage, but at this point I'm just curious why Gradle cannot find that class, which results in a compilation failure.

The message is

package com.sun.xml.internal.bind does not exist

Note that it compiles fine in eclipse as well as using the javac ant task. JAVA_HOME is set to 1.6.0_27.

So what is unique about Gradle that by default causes it to not find this class and how could someone workaround this issue?

See Java code snippet followed by build.gradle file:

final class Test{
    private static final javax.xml.bind.DatatypeConverterInterface DTC = com.sun.xml.internal.bind.DatatypeConverterImpl.theInstance;

    .....
    DTC.printDateTime(Calendar.getInstance());
}

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.foo.bar.Test"
sourceCompatibility = 1.6
version = '5.4.0'

jar {
    manifest {
        attributes 'Implementation-Title': 'Foo', 'Implementation-Version': version, 'Implementation-Vendor': 'Bar'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-lang', name: 'commons-lang', version: '2.4'    
    compile group: 'com.google.guava', name: 'guava', version: '10.0.1'    
    compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.0-api', version: '1.0.1.Final'    
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    compile group: 'jdom', name: 'jdom', version: '1.0'        
    compile group: 'log4j', name: 'log4j', version: '1.2.16'

    runtime group: 'antlr', name: 'antlr', version: '2.7.6'
    runtime group: 'backport-util-concurrent', name: 'backport-util-concurrent', version: '3.1'
    runtime group: 'c3p0', name: 'c3p0', version: '0.9.1.2'
    runtime group: 'cglib', name: 'cglib', version: '2.2'
    runtime group: 'commons-beanutils', name: 'commons-beanutils', version: '1.8.3'
    runtime group: 'commons-codec', name: 'commons-codec', version: '1.3'
    runtime group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
    runtime group: 'commons-io', name: 'commons-io', version: '1.4'    
    runtime group: 'commons-logging', name: 'commons-logging', version: '1.1.1'    
    runtime group: 'dom4j', name: 'dom4j', version: '1.6.1'
    runtime group: 'javassist', name: 'javassist', version: '3.12.0.GA'    
    runtime group: 'javax.transaction', name: 'jta', version: '1.1'        
    runtime group: 'org.slf4j', name: 'slf4j-api', version: '1.6.2'
    runtime group: 'xerces', name: 'xerces', version: '2.4.0'        
    runtime group: 'xerces', name: 'xercesImpl', version: '2.10.0'
    runtime group: 'xml-apis', name: 'xml-apis', version: '2.0.2'                
}

EDIT

C:\>gradle -v

------------------------------------------------------------
Gradle 1.0-milestone-3
------------------------------------------------------------

Gradle build time: Monday, 25 April 2011 5:40:11 PM EST
Groovy: 1.7.10
Ant: Apache Ant(TM) version 1.8.2 compiled on December 20 2010
Ivy: 2.2.0
JVM: 1.6.0_27 (Sun Microsystems Inc. 20.2-b06)
OS: Windows XP 5.1 x86

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

↙厌世 2024-12-17 02:22:37
  • 解决方案#1:

将 rt.jar 复制(而不是移动)到 \jre\lib\rt.jar 以外的位置
(例如 c:\rt.jar)并按如下方式运行编译器:

javac -cp c:\rt.jar your_file.java
  • 解决方案 #2:

使用编译器选项 -XDignore.symbol.file=true 进行编译

  • Solution #1:

Copy (not move) rt.jar to a location different than <JDK_install_dir>\jre\lib\rt.jar
(for example to c:\rt.jar) and run your compiler like below:

javac -cp c:\rt.jar your_file.java
  • Solution #2:

Compile with compiler option -XDignore.symbol.file=true

梦过后 2024-12-17 02:22:37

Gradle 还在下面使用 javac Ant 任务。使用仅包含 apply plugin: "java"build.gradle 编译类对我来说在 Mac OS 10.6 上使用 Gradle 1.0-milestone-3 效果很好。所以我最好的猜测是您正在使用 JRE 而不是 JDK 运行 Gradle,或者使用 1.5 而不是 1.6。

尝试将以下内容放入构建脚本中并查看它是否有效:

// only available in JDK 1.6
println javax.tools.ToolProvider.getSystemJavaCompiler() 

Gradle also uses the javac Ant task underneath. Compiling the class with a build.gradle that only contains apply plugin: "java" works fine for me with Gradle 1.0-milestone-3 on Mac OS 10.6. So my best guess is that you are running Gradle with a JRE rather than a JDK, or with 1.5 rather than 1.6.

Try to put the following into the build script and see if it works:

// only available in JDK 1.6
println javax.tools.ToolProvider.getSystemJavaCompiler() 
给不了的爱 2024-12-17 02:22:37

为什么不直接使用:

javax.xml.bind.DatatypeConverter.printDateTime(Calendar.getInstance());

why not just use:

javax.xml.bind.DatatypeConverter.printDateTime(Calendar.getInstance());
夜未央樱花落 2024-12-17 02:22:37

我最近遇到了同样的问题,您可以在这个 问题中找到更多信息

我通过引用这个 解决了这个问题,你可以看到包含 javax.xml.bind.DatatypeConverterInterface 在这里

我打包了该库并通过我的应用程序引用了它。

I had the same problem recently which you can find more information about in this question.

I solved it by referencing this library which you can see that javax.xml.bind.DatatypeConverterInterface is included here.

I packaged the library and referenced it by my application.

清风无影 2024-12-17 02:22:37

原因:
javac 使用不包括所有 Sun 专有类的特殊符号表。当 javac 编译代码时,默认情况下它不会链接到 rt.jar。相反,它使用带有类存根的特殊符号文件 lib/ct.sym。
解决方案:

compileJava {options.bootClasspath = "$System.env.JAVA_HOME/jre/lib/rt.jar"}

reason:
javac uses a special symbol table that does not include all Sun-proprietary classes. When javac is compiling code it doesn't link against rt.jar by default. Instead it uses special symbol file lib/ct.sym with class stubs.
solution:

compileJava {options.bootClasspath = "$System.env.JAVA_HOME/jre/lib/rt.jar"}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文