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.
Related
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.
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 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
I am trying to load a resource that is contained within an embedded JAR file. The project is actually deployed in JBoss using an EAR file with the following structure:
deploy.ear
|
|-> project.sar
|
|-> sub_project.jar
| |
| |-> settings.xml
|
|-> com/path/project/
|
|-> main.class
From main.java I'd like to get a InputStream for settings.xml. What is the correct way to do this?
My current understanding that the following code should work, but it is returning null:
this.getClass().getResourceAsStream("settings.xml");
Update
After some trial and error, the following statements work:
getClass().getResourceAsStream("/settings.xml");
getClass().getResourceAsStream("/sub_project.jar/settings.xml");
getClass().getClassLoader().getResourceAsStream("/settings.xml");
getClass().getClassLoader().getResourceAsStream("settings.xml");
getClass().getClassLoader().getResourceAsStream("sub_project.jar/settings.xml");
getClass().getClassLoader().getResourceAsStream("/sub_project.jar/settings.xml");
This might be a good resource: http://one-jar.sourceforge.net/version-0.95/
The main idea is that the inner JAR is not loaded by the ClassLoader that loaded the outer JAR automatically, you need to do so manually, e.g. by using a StreamClassLoader to load the inner jar
Only then, from your own ClassLoader you can get that resource using getResourceAsStream(...)
I'm having trouble when one of the jars that my web app depends on tries to load a properties file from within the jar. Here is the code in the jar.
static
{
Properties props = new Properties();
try
{
props.load(ClassLoader.getSystemResourceAsStream("someProps.properties"));
} catch (IOException e)
{
e.printStackTrace();
}
someProperty = props.getProperty("someKey");
}
The properties file is in my "src/main/resources" directory of the Maven project. When I run this code from my junit test in Eclipse, it executes just fine. When the project is built with Maven into a jar, and included as a dependency in my web app, it fails to locate the properties file. I know that the properties file is at the base directory of the depended on jar, I don't know how to fix this.
The problem is that you are using getSystemResourceAsStream. Use simply getResourceAsStream. System resources load from the system classloader, which is almost certainly not the class loader that your jar is loaded into when run as a webapp.
It works in Eclipse because when launching an application, the system classloader is configured with your jar as part of its classpath. (E.g. java -jar my.jar will load my.jar in the system class loader.) This is not the case with web applications - application servers use complex class loading to isolate webapplications from each other and from the internals of the application server. For example, see the tomcat classloader how-to, and the diagram of the classloader hierarchy used.
EDIT: Normally, you would call getClass().getResourceAsStream() to retrieve a resource in the classpath, but as you are fetching the resource in a static initializer, you will need to explicitly name a class that is in the classloader you want to load from. The simplest approach is to use the class containing the static initializer,
e.g.
[public] class MyClass {
static
{
...
props.load(MyClass.class.getResourceAsStream("/someProps.properties"));
}
}
For the record, this is documented in How do I add resources to my JAR? (illustrated for unit tests but the same applies for a "regular" resource):
To add resources to the classpath for
your unit tests, you follow the same
pattern as you do for adding resources
to the JAR except the directory you
place resources in is
${basedir}/src/test/resources. At
this point you would have a project
directory structure that would look
like the following:
my-app
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- com
| | `-- mycompany
| | `-- app
| | `-- App.java
| `-- resources
| `-- META-INF
| |-- application.properties
`-- test
|-- java
| `-- com
| `-- mycompany
| `-- app
| `-- AppTest.java
`-- resources
`-- test.properties
In a unit test you could use a simple
snippet of code like the following to
access the resource required for
testing:
...
// Retrieve resource
InputStream is = getClass().getResourceAsStream("/test.properties" );
// Do something with the resource
...