ryekayo
ryekayo

Reputation: 2421

Java is not finding the class

I have created a Manifest file:

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  

When I attempt to run the file in Windows Command Prompt in directory: C:\temp\workspace\DICOMVALIDATE>java -jar DICOMValidate.jar, I get this error:

   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$1.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

This should be accessing DicomInputStream in the dcm4che-core-2.0.25.jar. I have no idea why it is not accessing that class as I have attempted this many times and still get the same result (despite me having made sure spelling and everything is correct). Can someone please help me figure out why this .jar is not getting accessed?

UPDATE: I have made all the changes you recommended but I am still getting hit with the same error:

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$1.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

Can someone please help me figure out why it is not recognizing DicomInputStream?????

Upvotes: 1

Views: 1019

Answers (3)

Olivier Grégoire
Olivier Grégoire

Reputation: 35437

There are several issues in your MANIFEST.MF.

First, you have several library names that are incorrect, as pointed by ctrueden.

Second, your MANIFEST.MF must not have any line longer than 72 bytes (See ref).

So all in all, your MANIFEST.MF should be the following (tested locally):

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

Finally, as recommended by others, you should really use Maven. Here's the POM file as it should be to build the jar that you want.

<?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>

Upvotes: 2

ctrueden
ctrueden

Reputation: 6992

There is a typo in your Class-Path entry. You have lib/slfj-api-1.6.4jar but it should be lib/slf4j-api-1.6.4.jar. You also specify lib/log4j-1.2.16.jar but at least one dependency needs log4j-1.2.17.jar. The error message "The archive: /DICOMVALIDATE/lib/log4j-1.2.17.jar which is referenced by the classpath, does not exist" was a nice hint there.

In general, make sure you have all needed JAR files, and their dependencies, etc., on the classpath. Otherwise, you may get ClassNotFoundException even when the class itself is present but a dependency is not. I would heartily recommend using a build system with dependency management skills, such as Maven or Gradle, to keep all your ducks in a row more easily. Maven generates a manifest for you, so that you do not need to do it by hand.

Upvotes: 4

Crazyjavahacking
Crazyjavahacking

Reputation: 9697

The class will not be on the classpath, print the following expression from your program:

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

and it will print the classpath.

Upvotes: 0

Related Questions