1

I have an image I downloaded for a browse button. I put it in my src/main/resources folder in my maven project. In my Browse Action here is my code for the class:

package net.draconia.testcopier.ui.actions;

import java.awt.event.ActionEvent;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;

import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import net.draconia.testcopier.TestCopierController;

@Component
public class Browse extends AbstractAction
{
    private static final long serialVersionUID = 6237458116383473511L;

    private TestCopierController mObjController;

    public Browse(final TestCopierController objController)
    {
        super();

        URL objURL = Browse.class.getResource("./if_8_27829.png");

        putValue(SMALL_ICON, new ImageIcon(objURL));

        setController(objController);

    }

    public void actionPerformed(final ActionEvent objActionEvent)
    {
        getController().browseForBackupFolder();
    }

    protected TestCopierController getController()
    {
        return(mObjController);
    }

    protected void setController(final TestCopierController objController)
    {
        mObjController = objController;
    }
}

The problem is though I get a null in the constructor when I try to access the image through Browse.class.getResource(...). I've tried with "./" before the filename and without it there - just bare filename too and I get the same result. Here is my pom file:

<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>net.draconia</groupId>
    <artifactId>reedelseviertestcopier</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Reed Elsevier Test Copier</name>

    <properties>
        spring.version>4.3.2.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
</project>

I don't know if maybe the error is in my pom file why in eclipse I can't get it to see the image. I was about to try to implement my project to produce an executable jar so I can try running it in a command line to produce the output window and such but figured it should at least work in eclipse, you know? Any thoughts?

dsh
  • 12,037
  • 3
  • 33
  • 51
Seth D. Fulmer
  • 490
  • 1
  • 4
  • 21
  • https://stackoverflow.com/search?q=getResource+returns+null –  Aug 31 '17 at 17:05
  • https://stackoverflow.com/questions/13269556/strange-behavior-of-class-getresource-and-classloader-getresource-in-executa/ ? –  Aug 31 '17 at 17:07

1 Answers1

4

Class.getResource() searches through the CLASSPATH. Before it does so, it will construct the absolute resource name to use based on the value you passed in:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form:

    modified_package_name/name

    Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

So when you call Browse.class.getResource("if_8_27829.png"), the CLASSPATH is searched for /net/draconia/testcopier/ui/actions/if_8_27829.png, which doesn't exist.

You need to call Browse.class.getResource("/if_8_27829.png") and ensure that src/main/resources is in the CLASSPATH when your program is run. (I am not familiar with maven, so I don't know how it manages that)

dsh
  • 12,037
  • 3
  • 33
  • 51