I have my file path as follows:
+------------+ +-----------------+ +------------------+
| | | | | |
| src +-----------> com +--+---> application |
| | | | | | |
+------------+ +-----------------+ | +------------------+
|
| +------------------+
| | |
+---> resources |
| |
+------------------+
My code is in the application folder and my pictures that I want to load are in resources.
Im using the following code to get the images from resources onto a class in application (image is a BufferedImage).
image = ImageIO.read(new File("./src/com/resources/Pad.png"));
This seems to work in my IDE (intellij) as I can see the loaded images as shown
However, when I build, the images aren't being shown. I'm building by:
File -> Project Structure -> Artifacts -> Green plus -> JAR -> From modules with dependencies -> my main class location -> Extract to target Jar
Then I Build Artifact -> Build.
So when I go to the destination of the built jar and run it none of the pictures are being shown
I checked to see if the images were in the built jar by extracting the jar which does contain the pictures but for some reason the code isn't loading the picture.
You can try to use resourse as a stream instead of file.
image = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("com/resources/Pad.png"))
The src path is not available in build jar. You should use class loader to load resources from classpath. E.g. (assuming that 'src' is classpath root)
image = ImageIO.read(new File(this.getClass()
.getResource("com/resources/Pad.png").getPath());
Edited with the #MadProgrammer suggestion.
Related
I'm taking over a maven/spring application, where the configuration files are not on the classpath. i.e, the config folder is copied by the maven-assembly-plugin next to the jar, to ease the configuration when the application is deployed.
My problem is that I need these config files to run this app from within my IDE (intelliJ), and of course, Spring is complaining
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
The assembly takes either "application-prod" or "application-test".properties, depending on the maven profile used at packaging time, and copy it as "application.properties" into the /config folder, so next to the jar.
My question is:
What should I do to have this "application.properties" file available in the classpath (ideally depending on the maven profile), when running the application in Intellij, without messing around with the project structure ?
I guess that giving the properties file as a parameter to the launcher could be a solution, but I don't know how.
So here is the simplified project structure:
|-- src/
| |-- main/
| | |-- assembly/
| | | |-- bin.xml
| |
| | |-- config/
| | | |-- images/
| | | |-- application-prod.properties
| | | |-- application-test.properties
| |
| | |-- java/...
| | | |-- gui/
| | | | |-- Application.java
| |
| | |-- resources/
| | | |-- context/
| | | | |-- context.xml
| |
| | | |-- fxml/
| | | | |-- welcome.fxml
| |
| | | |-- gui.properties
| |
|-- pom.xml
Many thanks.
In this simple situation without multimodule dependencies it can make the trick.
You can right click on any directory in your IntelliJ project, select "Mark Directory As", and choose "Source Root" (or resource root as well). That directory folder will change color from yellow to blue (or resource icon will appear); IntelliJ will ensure that all those directories will be in your CLASSPATH.
Also take a note that you should not try to explicitly add smth to CP var in IDEA. Classpath is configured in the Module Dependencies of your module.
My application requires that a \config directory be available on the classpath when it looks for configurations files under the directory. I currently have dependencies configured like so, though this is probably not the correct way to make a directory available to my application:
dependencies {
... //runtime, compile dependencies pulled from repositories
runtime files('config')
}
I am using the application plugin to create a standalone zip for my project. If my \config directory has \config\subdir, file1, file2, then the plugin produces a build\install directory with the following structure:
| build
| --|install
| ----|bin
| ------ projectName
| ------ projectName.bat
| ----|lib
| ------ dependency1.jar
| ------ dependency2.jar
| ------|subdir
| ------ file1
| ------ file2
This does not work for my application because it explicitly expects a \config directory
However, this is the directory structure that I need:
| build
| --|install
| ----|bin
| ------ projectName
| ------ projectName.bat
| ----|lib
| ------ dependency1.jar
| ------ dependency2.jar
| ----|config
| ------|subdir
| ------ file1
| ------ file2
How can I make gradle add another directory to the build and specify it as part of the classpath for the generated startup scripts?
The application plugin documentation says:
Static files to be added to the distribution can be simply added to src/dist
I would try putting your config directory into src/dist/lib and continue adding it to your classpath with runtime files('src/dist/lib/config')
Note: working around this defect means that config has to go into /lib under src/dist
You may try this:
project('project-name') {
apply plugin: 'application'
mainClassName = "your.main.Class"
startScripts {
classpath += files('src/dist/lib/conf')
}
More information can be found here.
jar {
manifest {
attributes('Class-Path' : '<directory>' )
}
}
The above worked for me
I have a maven project in eclipse with App.java in in my.package in src/main/java and a couple of pictures in my.package.resources
I looked in the jar and the structure is like this:
MyApp
|- chrriis \\ library stuff
|- com \\ stuff with google and sun
|- javax
|- META-INF \\ contains MANIFEST.MF and other maven stuff
|- org \\ contains most of my used libraries
|- my
| |- package
| | | App.class
| | | App$1.class
| | |- resources
| | | |-picture.png
When I load a picture like:
JPanel p1 = new JPanel(new GridBagLayout());
p1.add(new JLabel(new ImageIcon(App.class.getResource("resources"+File.separator+"picture.png"))));
and run it in eclipse, it works fine. But when I Export it to a runnable jar it give me this error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
...
But when I move the picture.png to my.package and do
App.class.getResource("picture.png") it works fine.
I tried this four solutions but they did not work for me:
(1) (2) (3) (4)
Any help would be appreciated.
I have the following folder:
Game Of Life
|_bin
| |_Board.class
| |_Frame.class (main class)
| ...
|_res
| |_playToolbar.png
| |_pauseToolbar.png
| ...
|_src
|_Board.java
|_Frame.java
...
How do I create an executable .jar containing every class and image, so that when I execute the .jar it runs the Frame class? I am using Eclipse.
I think it is best to put your pictures inside a package. This way, everything will be packaged and loaded from your JAR file.
Afterward you will have to load your images using the getResource(...) method from an instance of Class.
You will have something like this:
GameOfLife
|- src
| |- my
| | |- company
| | | |- app
| | | | | Board.java
| | | | | Frame.java
| | | | |- resources
| | | | | |- playToolbar.png
| | | | | |- pauseToolbar.png
Then to create an ImageIcon of "playToolbar.png" from the Frame class you will have to use:
new ImageIcon(Frame.class.getResource("resources/playToolbar.png"));
Or:
new ImageIcon(Frame.class.getResource("/my/company/app/resources/playToolbar.png"));
If you are using Netbeans GUI builder, it can load resources from package without any problem.
To autorun your Frame class you have to create a MANIFEST.MF file and put it inside a folder named META-INF at the root of your JAR. Inside, you can define Frame as the Main-Class:
Manifest-Version: 1.0
Main-Class: my.company.app.Frame
Eclipse can do this step automatically if you select export->Runnable Jar.
If you are using eclipse, you can do a Rightclick->Export on the project and select Runnable Jar File under Java. Your res folder has to be configured as a source Folder in eclipse, otherwise it wont be exported.
You also have to regard that Loading the images may be different once they are packed in a jar file. See this for more information
Before migrating to maven, we used Ant and there were no problems with launching Dev Mode from under IntelliJ IDEA. But now the project is under Maven. So I made, I think, all needed configuration, and when I run debugger I see login page, but after logging in I receive the following error:
[ERROR] Resource img/remove_filter_btn.png not found. Is the name specified as Class.getResource() would expect?
Here is how I use resource:
public interface Resources extends ClientBundle {
#Source("img/remove_filter_btn.png")
ImageResource removeBtn();
}
The project structure is the following:
Module
|--Submodule
| |--src
| | |--main
| | | |--java
| | | |--package
| | | |--class
| | |--webapp
| | |--img
| | |--remove_filter_btn.png
| |--target
| |--ROOT
| |--img
| |--remove_filter_btn.png
|--utils
|--tomcat
|--webapps
|--ROOT
|--img
|--remove_filter_btn.png
War file is launched in tomcat that is located in utils folder.
In fact, I don't know where GWT looks for this resource. Maybe there's a solution on how to get the point where GWT starts looking resources for?
At the moment this is the only question because i think that it is the only left issue before debug would start working.
The images consumed by ClientBundle into ImageResources are expected to be in the source tree, not in the external web resources (i.e. in target/ or webapp/).
When you write
package my.project.something.client
//...
public interface Resources extends ClientBundle {
#Source("img/remove_filter_btn.png")
ImageResource removeBtn();
}
you are telling ClientBundle that it can find the image at src/my/project/something/client/img/remove_filter_btn.png.
Keep in mind that the purpose of ClientBundle isn't just to wrap up pathname strings so they can be easily used in code, but to actually compile the image into your app. In the case of small images, it will often actually put the entire image into the app so there is no need for any spriting at all. Even if this isn't possible, it will still rewrite the images so they can be cached forever by the browser, and try to optimize them to not use any more space than actually needed.