导入包编译为jar:坏类文件,类文件包含错误的类和包不存在错误

时间:2015-09-26 20:06:41

标签: java ant jar classpath packages

我有一个我创建的包,并使用ant将其编译成jar文件。

文件结构就像这样

project folder
  x_package
    compiled package_x.jar
    com
      name
        package_x
          Package files
  demos
    src
      files I'm trying to compile

我正在尝试编译demos / src下的文件,这些文件依赖于已编译的package_x.jar

我这样做是通过将package_x.jar添加到类路径中作为javac任务的参数

Javac任务

    <javac destdir = "${build.dir}" source = "${ant.java.version}" target = "${ant.java.version}" 
        debug = "true" deprecation = "false" optimize = "false" failonerror = "true" >
        <src path = "${source.dir}" />
        <classpath refid = "master-classpath"/>
    </javac>

主-类路径

    <path id = "master-classpath" > 
        <fileset dir = "${classpath.dir}" >
            <include name = "*.jar" />
        </fileset>
        <pathelement path = "${classpath.dir}" />
    </path>

每当我运行ant文件时,都会发生2次错误

error: package package_x does not exist

并在第一次引用包中定义的类

error: Cannot access class_name
bad class file C:/(path)/package_x.jar (class_name)
class file contains wrong class: package_x.class_name
please remove or make sure it appears in the correct subdirectory of the classpath
package_x包中的

每个文件都以

开头
package package_x;
/*code*/

并且demos目录中的文件有

import package_x.*;
/*code*/

任何赞赏的建议

1 个答案:

答案 0 :(得分:0)

我不确定这是否是唯一的问题,但是在包声明和导入中你必须编写完整的包路径,比如com.name.package_x