I'm trying to utilize a resource directory in a maven project on Netbeans 11. I can't properly access the files in the resources directory when I build my code using Netbeans—however, it works fine when I build and run the code using mvn via the command prompt.
More specifically, I'm trying to load an image, bern.png, that I have stored in a directory located at /src/main/resources. When I use the following code to try and access those resources, however, I run into a NullPointerException when toString() is called on the result of getResource():
String path = this.getClass().getClassLoader().getResource("bern.png").toString();
Here is the NullPointerException stacktrace:
https://pastebin.com/KqJVxWEL
However, when I build and run the project using mvn via the command line, the project builds without issue.
Thus, the problem must lie with the way that Netbeans is viewing the resources folder that I've created.
Here is an image of my project file tree:
And here is an image of my target build file tree:
I created the resources directory by creating a New > Folder in main. As you can see, bern.png in the resources directory does not populate in the target build.
After building and running the project using mvn via the command line, however, bern.png does populate:
I've found a few articles about this, but none of them have helped. This article from javaquery refers to Netbeans 8.0, and its instructions are no longer usable as written in Netbeans 11. This StackOverflow answer refers to Netbeans 7.2, and simply creating the directory as they suggested did not function for me in Netbeans 11. This StackOverflow answer was written for Netbeans 8.0 and also did not work.
With all of that said, does anyone know how to properly create a resources directory using NetBeans 11?
The issue wasn't that Netbeans was viewing the directory incorrectly—the problem was that Netbeans 11's default Maven build wasn't recognizing the directory correctly. Switching Netbeans's Maven system to the most recent binary from the Maven website addressed the issue.
I had the same problem using a Maven project under Netbeans 11.3 for the Netty client/server framework example http2/tiles. Because using the source files from netty-all-4.1.50.Final-sources.jar, the jpg resource files were located inside the directory io/netty/example/http2/tiles together with the java files. Copying this directory to the Netbeans Maven project's subdirectory src/main/java will result in the problem, that these resource files are not included in the target jar file.
The solution is to locate the resource files in a subdirectory main/resources as it is done in the Netty Github repository https://github.com/netty/netty/tree/4.1/example/src/main. Resource files in this location are automatically added to the target jat file when the project is build.
Related
I am looking into the github project located at,
SymbolicRegressionInJava. I am familiar compiling java code into jar file but not one huge project using Maven. I looked into Maven and it is another complex system that need to be figured out. The bin folder has already a compiled jar file. I used a command
jar -tf symbolic_regression_1.0.jar
to see the list of classes used for the project. It has both gp and ga directories. I added ga (from another github) folder where is gp folder is at ../src/.. Instead of Maven (which I cannot figure it out even with a tutorials), I attempted manually by typing
jar cvf test1.jar *
It compiled, however, it failed to run like symbolic_regression_1.0.jar. What I am missing here?
I'm trying to find more information on how eclipse handles Running a project.
I want to understand it more because I have an issue I'm currently having where apache-poi .jar files which have been included into the classpath of my project will work properly when the project is ran through eclipse, but will not be detected when going to the same projects folder and running the main jar file to start the program.
It gives me the error: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell (although sometimes instead of Cell, it's Sheet)
What could I consult to understand what is going on here, and possibly solve this issue?
Your NoClassDefFoundError indicates that the library was not available while running the jar.
This depends upon how you are exporting your project into the jar file.
If you're using eclipse to do so, you can:
Export->Java->Runnable Jar to create a jar that includes its dependencies
Make sure to check Package required libraries into generated JAR.
This will make all your jars (in build path including apache-poi.jar) as a part of the final jar.
It runs from eclipse because libraries are on the build path of the eclipse which makes them available in the classpath.
We have a Java project that was modified about 2 years ago based on the dates.
The project uses a the Play Framework which as I recalled built and ran just fine back in 2012.
The developer apparently had tried to change the project to be a Maven project but the folder structure is all over the place and not within the Maven src folder structure.
Attempting to run the Play commands does not work on building the project any longer and using the Maven commands does not compile the code.
What occurs is just the packaging of all of the folders and source code into a .jar file.
So the question is 'Can files outside of the Maven folder structure get compiled?' if so how, OR do I need to restructure all the code to be placed into the proper Maven folder structure to try and get this to work again?
Thanks for your time.
Rough view of the folder tree below: Unable to post the POM as it is on another system
Project Name
src
main/java/
trunk
Project Name
... play framework folder structure in here eclipse, modules, precompiled, tmp
conf
lib
Web Content
META-INF
WEB-INF
Yes you can compile files in a non-standard Maven folder structure. Maven natively supports multiple source directories for the purposes of generated sources.
Read the Maven use guide When You Can't Use the Conventions
Using Multiple Source Directories This occurs when you are producing a
single JAR (or other artifact), and have several source directories
with classes you want to include.
This answer shows how to edit the directory structure in Maven by specifying the appropriate properties to override from the superpom.
The Maven pom docs show the build element set mentioned in the link above.
As a side note this answer covers a non standard directory layout for building war.
New to NetBeans dev here. When I first created my project and began compiling, I noticed that NetBeans had automatically created a dist/ directory under my project root and was creating an Executable JAR in that directory.
I've now been developing for several weeks, and added many resource files to my project, including a system log file, an embedded SQLite database, and several properties files. These are files that my program (a Swing app) needs to read/write from at runtime in order to function correctly.
I just noticed this morning that the Executable JAR in my dist/ directory is several weeks old, and that compiling my project doesn't update it. I am wondering if that's because there are now all these new resource files and NetBeans doesn't know how/where to package them for the automatically-created Executable JAR.
Does this mean that I'll have to tweak the build.xml myself? Or is there a NetBeans dialog I can get to where I can specify where to pacakge everything? And if NetBeans should be doing this automatically, what could be the reason that it stopped creating the JAR for me, and what can I do to force this functionality again?
I went into Project Properties >> Packaging and made sure that the JAR file directory was still dist/MyProgram.jar and that the checkbox Build JAR after compiling was still checked, so obviously something else is either breaking the build or messing with NB.
Thanks for any tips!
If "Compile on Save" is enabled, then your .jar file will only be generated when you do a "Clean & Build" (Just "Build" is not available when "Compile on Save" is enabled)
You need to Clean and Build within netbeans to update the file within your /dist directory.
I use Netbeans 7.0 and this solution works for me.
I have installed Red5 server successfully and also am able to run the demos fine. Now, I want to create a sample red5 server application. I created a sample project according to the specific directory structure that Red5 requires. But, now when I try to open this project in Netbeans 6.8, I am unable to because both have a different directory structure. So, Netbeans doesn't consider it as a project. I actually want to convert this project to a war file, so I can deploy it to red5/webapps directory and then red5 deployer service can make project out it automatically. How do I convert this project to a war file? because in Netbeans I am unable to open it. Please help.
I don't know if this will fully answer your questions but this is my configuration.
In the main red5 directory there is a file called project.zip, you can open it to get basic configuration files, directories structure and build files for ant (Pure Java build tool).
Using netbeans, you create a new free-form project and point it to the directory of your project. because of the ant build files the project will be recognized.
Netbeans provides it's own ant version but i prefer to install and use ant from command line.
when you'll run ant in the project directory it will try to find dependencies and probably at first will complain that it cannot find ivy.
Ivy is a dependency manager that red5 project uses that can be found in the following URL: http://ant.apache.org/ivy/download.html
please notice that ant will tell you exactly where it searches for the required jar file, just download the ivy zip, unzip it, and place the jar inside it in the requested directory.
once ant will compile the project properly (just by running the commant ant), it will also create a war file under dist directory.
don't forget to add to your red5 netbeans project the relevant red5 jars in order for the project to compile properly.
I am also prefer use ant from command line. I have two directory structures. One for source files and another for deployment.
This example helped me to start.