Java 没有找到 class

Java is not finding the class

我已经创建了一个清单文件:

Manifest-Version: 1.0
Main-Class: DICOMValidate.menu
Class-Path: lib/log4j-1.2.16.jar lib/sl4j-api-1.6.4jar lib/sl4j-log4j12-1.6.4.jar lib/dcm4che-audit-2.0.25.jar lib/dcm4che-core-2.0.25.jar lib/dcm4che-image-2.0.25.jar lib/dcm4che-imageio-2.0.25.jar lib/dcm4che-iod-2.0.25.jar lib/dcm4che-net-2.0.2h.jar  

当我尝试 运行 目录 Windows 命令提示符中的文件时:C:\temp\workspace\DICOMVALIDATE>java -jar DICOMValidate.jar,我收到此错误:

   View DICOM tags
    Enter DICOM File Path Name:
    C:/Ryan.dcm
    Enter Tag list:
    C:/testing.txt
Exception in thread "main" java.lang.NoClassDefFoundError: org/dcm4che2/io/DicomInputStream
        at DicomValidator.viewDICOMTags.readDICOMObject(viewDICOMTags.java:40)
        at DicomValidator.menu.showMenu(menu.java:46)
        at DicomValidator.menu.main(menu.java:14)
Caused by: java.lang.ClassNotFoundException: org.dcm4che2.io.DicomInputStream
        at java.net.URLClassLoader.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
        ... 3 more

这应该是访问 dcm4che-core-2.0.25.jar 中的 DicomInputStream。我不知道为什么它没有访问 class 因为我已经尝试了很多次并且仍然得到相同的结果(尽管我已经确保拼写和一切都是正确的)。有人可以帮我弄清楚为什么这个 .jar 无法访问吗?

更新: 我已经进行了您建议的所有更改,但我仍然遇到同样的错误:

Please Enter:
1 to View DICOM Tags
2 to Add DICOM Tags
3 to Edit DICOM Tags
4 to exit
1
View DICOM tags
Enter DICOM File Path Name:
IM-0009-0001.dcm
Enter Tag list:
tags.txt
Exception in thread "main" java.lang.NoClassDefFoundError: org/dcm4che2/io/DicomInputStream
        at DicomValidator.viewDICOMTags.readDICOMObject(viewDICOMTags.java:40)
        at DicomValidator.menu.showMenu(menu.java:45)
        at DicomValidator.menu.main(menu.java:14)
Caused by: java.lang.ClassNotFoundException: org.dcm4che2.io.DicomInputStream
        at java.net.URLClassLoader.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
        ... 3 more

谁能帮我弄清楚为什么它不能识别 DicomInputStream?????

class 不会在 class 路径上,从您的程序中打印以下表达式:

((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs();

它将打印 class 路径。

您的 MANIFEST.MF 中存在几个问题。

首先,正如 ctrueden 指出的那样,您有几个库名称不正确。

其次,您的 MANIFEST.MF 的任何行不得超过 72 个字节 (See ref)。

总而言之,您的 MANIFEST.MF 应该如下(本地测试):

Manifest-Version: 1.0
Main-Class: DICOMValidate.menu
Class-Path: lib/log4j-1.2.16.jar lib/slf4j-log4j12-1.6.4.jar lib/slf4j
 -api-1.6.4.jar lib/dcm4che-audit-2.0.25.jar lib/dcm4che-core-2.0.25.j
 ar lib/dcm4che-image-2.0.25.jar lib/dcm4che-imageio-2.0.25.jar lib/dc
 m4che-iod-2.0.25.jar lib/dcm4che-net-2.0.25.jar

最后,按照别人的建议,你真的应该使用Maven。这是构建所需 jar 的 POM 文件。

<?xml version="1.0" encoding="UTF-8"?>
<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>tmp</groupId>
    <artifactId>tmp2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <repositories>
        <repository>
            <id>dcm4che</id>
            <url>http://www.dcm4che.org/maven2/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>dcm4che</groupId>
            <artifactId>dcm4che-audit</artifactId>
            <version>2.0.25</version>
        </dependency>
        <dependency>
            <groupId>dcm4che</groupId>
            <artifactId>dcm4che-core</artifactId>
            <version>2.0.25</version>
        </dependency>
        <dependency>
            <groupId>dcm4che</groupId>
            <artifactId>dcm4che-image</artifactId>
            <version>2.0.25</version>
        </dependency>
        <dependency>
            <groupId>dcm4che</groupId>
            <artifactId>dcm4che-imageio</artifactId>
            <version>2.0.25</version>
        </dependency>
        <dependency>
            <groupId>dcm4che</groupId>
            <artifactId>dcm4che-iod</artifactId>
            <version>2.0.25</version>
        </dependency>
        <dependency>
            <groupId>dcm4che</groupId>
            <artifactId>dcm4che-net</artifactId>
            <version>2.0.25</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>DICOMValidate.menu</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

您的 Class-Path 条目中有错字。您有 lib/slfj-api-1.6.4jar,但应该是 lib/slf4j-api-1.6.4.jar。您还指定了 lib/log4j-1.2.16.jar,但至少需要一个依赖项 log4j-1.2.17.jar。错误消息 "The archive: /DICOMVALIDATE/lib/log4j-1.2.17.jar which is referenced by the classpath, does not exist" 是一个很好的提示。

通常,请确保您在 class 路径中拥有所有需要的 JAR 文件及其依赖项等。否则,即使 class 本身存在但依赖项不存在,您也可能会得到 ClassNotFoundException。我衷心推荐使用具有依赖管理技能的构建系统,例如 Maven 或 Gradle,以便更轻松地让所有鸭子保持一致。 Maven 为您生成一个清单,这样您就不需要手动操作了。