简体   繁体   中英

Importing contents of jar file not working

Sorry for the very nooby and simple question but this has been driving me mad all day:

I am trying to create a simple program that will help me sort some jpg files into folders. I want to use metadata-extractor to extract the date the photos were taken ( https://code.google.com/p/metadata-extractor/ ).

My problem is whenever I try to compile, it says the packages do not exist. I have the jar file in the same directory as my test java file, along with the picture I'm trying to test on.

I set up the classpath in the Git Bash as follows:

javac -cp "metadata-extractor-2.6.4.jar:." PictureSort.java

along with the (few simple) lines of code:

import java.io.File;
import com.drew.metadata.Metadata;

class PictureSort
{
    public static void main(String[] args) 
    {
         File file = new File("IMG_1244.JPG");
         Metadata metadata = ImageMetadataReader.readMetadata(file);
    }
}

I have examined the contents of the Jar file for what I need and Metadata.java is contained in com/drew/metadata/Metadata so thats why I used said import statment on the second line.

But... I am getting the following errors:

PictureSort.java:2: error: package com.drew.metadata does not exist
PictureSort.java:9: error: cannot find symbol
     Metadata metadata = ImageMetadataReader.readMetadata(file);
            ^
 symbol:   class Metadata
 location: class PictureSort
 PictureSort.java:9: error: cannot find symbol
     Metadata metadata = ImageMetadataReader.readMetadata(file);
                                ^
 symbol:   variable ImageMetadataReader
 location: class PictureSort
 3 errors

I have tried looking for an answer but I can't seem to find a solution that will work for me. Can anyone point me in the right direction for what I'm doing wrong? I don't have much experience in the side of utilising jars. Help is really appreciated.

I believe it's source.com.drew.metadata . Cause package name always starts with a lowercase letter. Also I don't think package name is like that. Try com.drew.metadata .

A quick check of the Javadoc for Metadata and you'll see at the top that Metadata is in package com.drew.metadata

That said, you appear to have a source jar, not the compiled classes:

javac -cp "metadata-extractor-2.6.4-src.jar:." PictureSort.java

You need to download the .jar containing the compiled classes. It appears you can do so from here: https://code.google.com/p/metadata-extractor/downloads/detail?name=metadata-extractor-2.6.4.zip

Download the .zip , unzip it, get the .jar

Once you've done that, your import will be:

import com.drew.metadata.Metadata;

And you'll compile via:

javac -cp "metadata-extractor-2.6.4.jar:." PictureSort.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM