简体   繁体   中英

Maven project - load file from outside of resources folder (project root)

I am creating a contacts application using Maven in Netbeans. For the operation of the program I want users to add and store images (contact avatars) in a folder /avatars and access them on a listener event. I can access images from within the ProjectRoot/src/main/resources/images directory, but cannot access ProjectRoot/avatars. Note: I do not want to store avatars in the resources directory because these will be user-added during the programs operation.

I have tried using getClass().getResource(avatarPath); as suggested in similar questions, but it has not worked. I have also tried adding the "avatars" directory to the POM as its own resource directory, but that has not worked either.

How can I access files/folders in the root directory of my project when using Maven?

listviewContacts.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Contact>() {
        @Override
        public void changed(ObservableValue<? extends Contact> observable, Contact oldValue, Contact newValue) {
            String avatar = newValue.getAvatar();
            String avatarPath = null;
            if (avatar.isEmpty()) {
                avatarPath = "/images/" + DEFAULT_AVATAR; // loads from ProjectRoot/src/main/resources/images
            } else {
                avatarPath = "/avatars/" + avatar; // won't load from ProjectRoot/avatars
            }
            try {
                imageviewContact.setImage(new Image(avatarPath));
            } catch (IllegalArgumentException ex) {
                System.err.println("Could not locate " + avatarPath);
            }
        }

    });

You are mixing 2 different things.

You can either have a classpath resource packed in jarfile along with your classes, or in a directory that is explicitly added java to java classpath(using java -cp commandline argument). That can be accessed via getClass().getResource.

Or you can load a file from arbitrary location, using java.io.File. Then your "projectRoot" is some folder in a filesystem, that you can either hardcode, configure by -DprojectRoot=C:/fun/with/files, or use some of relative paths. Maven has nothing to do with it, as avatars "will be will be user-added during the programs operation".

Your users will not launch your IDE, right ?

The problem was in understanding where the different constructors of javafx.scene.image.Image begin their path.

When using the URL constructor for Image, the URL path will start in Maven's defined resources folder (/src/main/resources or whatever additional resource directories were added to the pom.xml file ):

// the supplied string invokes the URL constructor
Image image = new Image("path/to/file");  

When using the InputStream constructor for Image (via FileInputStream), the path will start at the root directory of the project/application:

// the FileInputStream invokes the InputStream constructor
Image image = new Image(new FileInputStream("path/to/file")); 

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