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.
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 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
I want to pack jar file without containing depedency jar libraries using eclipse IDE. I used these steps to pack but the result jar file always contains jar libraries.
Some steps I did
Jar file I want to pack is jar library not runable jar file
Thanks
In eclipse, when you are creating a jar file, select option named 'JAR file' and not 'Runnable JAR'. When you click on next, you will get a list of items which you need to include while creating your jar file with a check box in front of each item. Just don't select the items you don't want to export (in your case dependent libraries).
Hope this helps.
Under Export, click Jar file and not Runnable Jar file
I would use an ant task...
<zip destfile="${backupJar}" update="true" basedir="${basedir}/../../"
includes="**/*.xml*, **/*.java"
compress="true"/>
Remember that a jar is really a zip file with a different extension.
See
http://ant.apache.org/manual/Tasks/zip.html
EDIT: you specifically asked how to ignore dependencies. This is achieved by using the excludes attribute of the zip task, for example:
<zip destfile="${backupJar}" update="true" basedir="${remotedir}/../"
excludes="**/lib/testonly.zip,
**/lib/ms*.jar,
**/*.tmp"
includes="exported/**/*.*, exported/jf123/**/*.*" compress="true"/>
I have a property file for a client project. I want to export the client as runnable jar, but the properties file should not be integrated into this jar. Because I want of course make edits to it, without having to recompile the jar client file.
How can I export it this way with eclipse?
I'm not currently using Java, but i once used Eclipse to export a runnable JAR. I used the Properties to store the settings and the file was generated automatically in the surrounding folder of the Jar-File. I don't know if this helps you, but it might be a point to start with....
You have to either put them in your project workspace so it is inside the jar (but this is not the case since you do not want that) or load them from a predefined directory, for example you could demand properties file to always be in the same directory as the runnable jar file.
By the way, you don't have to recompile your project if you put your properties file inside a jar. A jar file is just a package, you can unpack it, make edits to your properties file and then pack it again. It will work just as you demand.
If you change your mind, see this post.
If you want the properties file in the same directory as your JAR file, just add an extra step to your Ant build to zip up the properties file and the JAR file.
If not, have your Java code create the properties file where you want it the first time the Java application is run.
It does not seem to be possible to do this using standard Eclipse's export as runnable JAR (right click project -> Export -> Runnable JAR file -> Save as ANT script). However, Eclipse allows you to store the definition of your export as an Ant file, that one can modify later on.
The modification can look like below (I marked the modified parts with comments; it should be easy to compare with a script that your Eclipse generated).
<target name="create_run_jar">
<jar destfile="/mytargetlib/myjar.jar">
<manifest>
<attribute name="Main-Class" value="Main"/>
<!-- Here we only add lib/properties, the rest should be pre-generated by Eclipse -->
<attribute name="Class-Path" value=". lib/myjar1.jar lib/myjar2.jar lib/properties"/>
</manifest>
<fileset dir="/bin" excludes="**/*.properties" />
</jar>
<delete dir="/mytargetdir/lib"/>
<mkdir dir="/mytargetdir/lib"/>
<copy file="myjar.jar" todir="/mytargetdir/lib"/>
<copy file="/myprojectdir/lib/myjar1.jar" todir="/mytargetdir/lib"/>
<copy file="/myprojectdir/lib/myjar2.jar" todir="/mytargetdir/lib"/>
<!-- Again, all the above was generated by Eclipse. We only add a copy of our property file -->
<mkdir dir="/mytargetdir/lib/properties"/>
<copy file="/myprojectdir/properties/mypropertyfile.properties" todir="/mytargetdir/lib/properties"/>
<!-- end of our addition -->
</target>
I'm using NetBeans to import a series of libraries from my Arduino IDE. I'm following directions from the following link:
http://silveiraneto.net/2009/03/01/arduino-and-java/
This works provided I use the Arduino-0013 version of the IDE install, more current versions do not compile using this method.
I have found that using the Arudino-0013 set as the working directory is NOT necessary if I manually move the "preferences.txt" and "keywords.txt" and "librxtxSerial.so" files into the lib folder in my Java dist (build) folder, and also move the entire Arduino-0013 "Hardware" folder also into my Java dist (build) folder.
When I do this I can run the Java program from the dist directory on the command line. Using the command:
java -jar myProgram.jar
rather than having to go into the Arudino-0013 as my working directory and use -cp to get my program to work (which I haven't worked out how to do incidentally):
Is there a way to include these .txt files and the Arudino hardware folder with all the files it contains when I build the project with NetBeans? The reason I ask is because it's getting annoying having to do this manually every time I do a new build.
My answer is not specific to netbeans but you can try:
Make an Apache ANT build file to build your project. In that file make a copy task which will copy txt files in your build. By doing this you will not have to so it manually. See here: http://wiki.netbeans.org/NetbeansedAnt to know how to work with ANT in NetBeans.
In Netbeans, if you switch from the Projects tab to the Files tab, you'll see that you have a build.xml file. This is an Ant build.xml file. You can configure Ant to automatically copy the files for you whenever your build your project. You would essentially have something like this:
<project ...>
<target name="-pre-compile">
<copy file="some/path/preferences.txt" todir="../some/dir"/>
<copy file="some/path/keywords.txt" todir="../some/dir"/>
<copy file="some/path/librxtxSerial.so" todir="../some/dir"/>
</target>
<target name="-post-compile">
<copy todir="build/dir">
<fileset dir="some/path/Arduino-0013"/>
</copy>
</target>
</project>
There is more information in the build.xml file about what you can and can't hook into. The Ant documentation is good, and the Tasks section of the Ant Manual will be particularly useful.
There should be a dependencies section in NetBeans for your project. You can then add external libraries to your project, such as local JAR files you've got. The best way would probably be to jar up the text files and Arduino directory together, then add that JAR file as a compile-time (and/or run-time) dependency to your project. Now, when you compile your project in NetBeans it should include the specified JAR file on the classpath and voila.
Sorry to not give you more NetBeans-specific direction, I've only used the IDE a couple of times, but all IDEs will allow you to add local JAR files and third-party libraries as dependencies to your project, you just need to find where in the IDE you can do that.
Another idea that might work is to set NetBeans to use your local copy of Java for compilation instead of the one that came bundled with the IDE, that way you don't need to fuss with project dependencies. Again, I don't know where in NetBeans to set this, but start in the General Settings (or perhaps the Project-specific Settings) and find the Java/compilation section; hopefully there's an option to specify which JDK to use, then point it at your local copy.