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
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.
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 am stuck with creating stubs for my simple RMI implementation. With command line, I am at directory, where I have my class files stored.
For me it is: C:\Users\John\Documents\NetBeansProjects\testServer\build\classes\RMI
RMI here is the package I have my files in. Now when I am in this folder, I tri to make stubs with rmic -v1.2 -keep CountingOnServer, but command line writes me, that class CountingOnServer not found. No matter what I am doing, still it is writing such a unbelivable mistake... do you know how to solve that?
Thx
As a guess, issue is that CountingOnServer is inside some package, so you should use full name like RMI.CountingOnServer. Don't forget to run rmic from the the class-path root which in your case seems to be build\classes directory.
If you have your RMI implementation in packages run rmic at the root of your class files,
ex,
|-- build
| |-- classes
| | `-- com
| | |-- client
| | | `-- TestRMI.class
| | |-- rmi
| | | |-- Hello.class
| | | |-- HelloInterface.class
| | | `-- Hello_Stub.class
| | `-- RMTServer
| | `-- RmiServer.class
I have my classes in build/classes/com/rmi directory so, to create stubs I can run rmic at the root as,
../build/classes/rmic com.rmi.Hello