0

I`m developing simple java application with visualization. I have to package it to jar with maven. My project structure is:
src
---main
--------java
-----------com.chess
--------resources
-----------Images

I tried to use images like : Images\image.jpg but its missing. How i can use images?

This is my pom.xml:

<?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>ChessApp</groupId>
<artifactId>ChessApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.chess.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Srw
  • 69
  • 10

1 Answers1

1

By your comment,

layout.setIcon(new ImageIcon("Images\\BlackRockAndBlackBoard.jpg"));

ImageIcon treats the string as a filename, you need to take a gander at Get a resource using getResource()

Then use the url for constructing the ImageIcon.

URL imageUrl = YourClassName.class.getResource("/Images/ImageName.jpg");
layout.setIcon(new ImageIcon( imageUrl ));

The URL is allowed as a constructor argument as you can see here http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.net.URL%29.

Community
  • 1
  • 1
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • Yes this is answer. What other ways to choose "main" class in jar? Without plugin. – Srw Nov 27 '13 at 20:17
  • @Srw, you can just edit your manifest.MF by using the jar tool, resign it if necessary. If you have another question you should ask it as a new question here on SO. – Captain Giraffe Nov 27 '13 at 21:21