I want to include resources fils (images, textfiles...) in a JAR.
I added my asset folder to the classpath, so my program works fine when I run it in netBeans.
But if I build the project, I receive an error :
Not copying library C:\Users\Flow\Desktop\cp , it's a directory.
Is it a normal error ?
So I tried to add assets manualy like that :
<target name="-post-jar">
<jar destfile="dist/MonProjet.jar" update="true">
<fileset dir="C:/Users/Flow/Desktop/cp">
<include name="assets/*"/>
</fileset>
</jar>
</target>
But it does not work. Do I something wrong ?
Sorry for my english and thank you for help.
Don't place the files on your desktop. Go to the Source folder of your project and copy it in that folder. In NetBeans, any resources you place in the src tree will be automatically copied over to the classes tree when you do a Build (or Clean and Build), except those files matching the "Exclude From JAR File" skeletons specified in Project -> Properties -> Build -> Packaging. So if you pasted it there it should be in your jar file.
Your directory structure should now look like this:
Project0
-build
--classes
---asset
---project0
--empty
-nbproject
--private
-src
--asset
--project0
-dist
-test
Source: http://www.coderanch.com/t/511437/java/java/Adding-images-Jar-Files-NetBeans
Related
Sorry if the question is basic. I’m trying to build a java project that include text files :
File file = new File("data/include/foo.txt");
Problem is that with Netbeans, Built and Clean actions are done without taking into account external text files.
Can you please help me solving this problem.
In Netbeans there are basically two types of java projects. One is using ANT as its build machine and one is using maven.
Using ANT to build:
You have to modify or build a build.xml. Netbeans offers some targets in its standard build.xml to trigger events on each part of the build process.
So to include some additional resource files (in your case some text files) you should add your build.xml with a target like
<target name="-post-jar">
<jar destfile="dist/myjar.jar" update="true">
<fileset dir="${basedir}">
<include name="data/include/*"/>
</fileset>
</jar>
</target>
It means that after your JAR is build the files from ${basedir}/files/* are included as well.
Using MAVEN to build:
Netbeans uses here completely Mavens infrastructure. So Maven does have a standard mechanism to recognize resource files to include. It all comes down to place these files in a specific place of the project. Assuming you did not change this standard directory layout of maven JAR projects is is something like:
Project
src
main
java
resources
...
So all files placed in src/main/resources (including subfolders) are included in your JAR automatically.
Read more here
Edit 1:
To access this resource files you cannot use File objects. This is done using the class own getResource methods:
App.class.getResourceAsStream("data.xml")
App.class.getResource("data.xml")
Here my class is App and my datafile is data.xml which is in the same directory as my class. This is relative adressed. But if you use a path with a heading / then your path is JAR file absolute. Read more about this here.
I have the initial code generated by netbeans in the new JFrame Form which shows the following output when run:-
But, when I run the jar file in the dist folder, I get the following:-
How, to solve this problem? Any help will be appreciated.
You will need to edit your build.xml so that all your used libraries are included in the build process.
This will look something like this in build.xml:
<target name="-post-jar">
<echo>Adding jar: ${reference.myMissingJar.jar}</echo>
<jar update="true" destfile="${dist.jar}">
<zipfileset src="${reference.myMissingJar.jar}"/>
</jar>
<mkdir dir="bin"/>
<copy file="${dist.jar}" todir="bin" description="Copy jar file."/>
</target>
The problem is that while you try to run it in netbeans, the IDE knows about all those nifty libs you use. When using a single jar, that is something different altogether.
Alternativly you might want to state the locations of your used libs in your classpath parameter.
In netbeans do a Clean and build project (shift + f11)
if that doesnt help please post more details.
Are u using external libs?
I have a java project . I want to export it to runnable jar file.
I use eclipse to do it.
But when i run created jar file i receive Error ~ file not found : config\file.xml (the system cannot find the path specified).
How can I export a folder to run success jar file at any where ?
If you don't need your config to be configured outside of jar, just include it in there and access with something like getClass().getClassLoader().getResourceAsStream("config.xml") if it's located at the level of "root" package in jar
Sounds like your application is looking for a configuration file using a relative path in the file system. In order to make the jar totally self-sufficient, the code would have to be modified to look for the config file in the classpath, and the file would have to be included in the jar.
To do this, the code that opens the file must be changed to use Class#getResourceAsStream() instead of using a File object.
I'd suggest you use ant or another build system. Here is a short tutorial on ant:
http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html
Its pretty easy to create an jar file using xml in your ant build script. I have done it many times:
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
Others may suggest maven and the like, but in reality they are all good choices. Exporting from an IDE such as eclipse really isn't a viable long term solution.
if you have a config-folder in your projects directory, and there is a file you want to read from, you have to copy this folder to the directory of your jar-file too
My professor asks us to submit out projects in WAR files, and he says my project will get a 0 if the WAR doesn't include my .JAVA file.
I see it includes the .CLASS file but he said thats not enough, how can i get the .WAR file to pack with the .JAVA inside of it?
EDIT:
To compress the file i'm using the clean and build option in Netbeans. the WAR file appears in the build folder
If this is in Eclipse, you will include source under the export WAR option.
Update
In the war export panel, there should be an "Excluded file types" make sure to remove *.java
WAR file is just an archive, understandable with any ZIP archiver, like 7-zip. You can put anything you want into WAR.
Look for section "Including Source Files in the WAR File"
http://users.cis.fiu.edu/~downeyt/webdev/netbeans.html
quoted from the link:
From the properties screen, select Build -> Packaging. Edit the Exclude From WAR File box and remove */.java,.
What are you using to create the WAR file? If you use apache ant then add a copy task to your build.xml and then reference it in the default target e.g. with <target name="dist" depends="copy-source"/>
The task might look something like:
<target name="copy-source">
<echo>Copying source files...</echo>
<copy todir="${app.dir}/src">
<fileset dir="${javasrc.dir}">
<include name="**/*.java"/>
</fileset>
</copy>
</target>
ps. It is best to define the variables in an external build.properties file and never use hard-coded values in build.xml.
Also, have a look at http://ant.apache.org/manual/Tasks/war.html and http://ant.apache.org/manual/Tasks/copy.html
In Netbeans.Right click your project. Select Project Properties.It will open a Project properties window. Select Build->Packaging from the Categories Tab. In the window that appears on right hand side, there is a text field: Exclude from WAR file. In that text field default value is: /.java,/.form. In that text field remove the */.java value and click Ok.
Now clean and build project. Now you will find the war file exported with .java sources under dist directory of your project.
i'm using netbeans for writing a webapp + filter.
I want to generate a jar file (to place in tomcat/lib folder) for the sake of the filter.
when i compile the project, it generates war file.
is there a way to tell netbeans to generate a jar file instead?
You can add something like this to your build.xml:
<target name="-post-compile">
<jar destfile="${basedir}/dist/my_web_app.jar">
<fileset dir="${basedir}/build/web/WEB-INF/classes">
</fileset>
</jar>
</target>
Which will build you a jar alongside your war.
EDIT: See Ravindra's answer below for a slight improvement to this method.
An enhancement to #DeadPassive answer - You can get the web app name using the variable ${ant.project.name}
<target name="-post-compile">
<jar destfile="${basedir}/dist/${ant.project.name}.jar">
<fileset dir="${basedir}/build/web/WEB-INF/classes">
</fileset>
</jar>
</target>
Create a separate project to generate your .jar file. In that case also I think you have to create the .jar using terminal. You can't create a .jar file for a web project using netbeans IDE, as far as I know.
Maybe modify the build.xml used by netbeans, adding a copy task on the generated jar.