How to update xml file in war file? - java

I have war which working in production. I need to change the web.xml and again i need to make war file to deploy.Since we are changing xml file, so no need compliation at all..But i don't have source code to create war file again.
if change the xml file which will work again ? if yes how to create war file in command prompt ?

WARs are just zip files with .waras extension, so you just have to :
extract the WAR into some directory
replace web.xml in WEB-INF
zip the directory content into a file using the same filename than before.
BTW, try to deploy your repackaged WAR into some production-like environment to test if application is running like you want.

Related

Supply properties file for jar file in Websphere

I have deployed a Spring Web based application in WAS. So WEB-INF/lib contains lot of jar files. One of the jar file trying to load properties file. Means that java file loading test.properties file by using getClassLoader().getResource().
How to supply that test.properties to that jar file ?
Please help me.
Thank you.

Why my file directory cannot be found after deploying the project thru a war file

I deployed a Java EE project thru a .war file and now it's uploaded online but the new File("directory") is missing. How to properly set the path of a file?
The current path is at
"/home/jebard/chabacano/Chabacano1/src/en-pos-maxent.bin"
After deploying your application can't access to your file system, it has an access only to content of .war file. So, to get access to some file you have to include it to .war archive while building.

Difference between deploying WAR and Build folder

Question 1: May I ask what is the difference between deploying a java webapp with it's WAR file vs just copy/pasting the build folder into tomcat webapp folder?
Question 2: Somehow I am told to deploy my project just by renaming my /build/web folder to /build/, then copy and paste this folder into tomcat/webapp folder. Tomcat did serve the web app and I could access it via url. But the problem is that I suspect my System variables were not set. I start up a servlet and put this code in this init(ServletConfig config) method:
System.setProperty("LogPath","D:/Test/logs");
And doing this in my log4j.properties
log4j.appender.file.FILE=${LogPath}/wrapper.log
wrapper.log is not found in the designated directory but a stdout.log is found in tomcat/logs folder.
I am sure the init() method was fired because I have a quartz scheduler there. I am suspecting that my System.setProperty was not set. Any hint?
Update: With all the same source code, I have no problem if I am deploying with a WAR file. The ${LogPath} in log4j.properties work as expected.
Let me answer you the first question.
WAR file is a zip archive with different name. When you deploy this file to the Tomcat server, it unpacks this file to its folder as you would do it by copy-paste. If you are just developing your own project in your own environment and you don't want to distribute it, you don't need to create a war file. But if you want to distribute this project, I recommend you to create a war file. One file is easier to be sent.
Read more on Wikipedia http://en.wikipedia.org/wiki/WAR_%28file_format%29

How to acces txt file in netbeans build jar file?

So currently my netbeans project folders looks like this:
Block_Breaker <--Project
build
dist
Block_Breaker.jar
nbproject
src
packageONE
packageTWO
data.txt
manifest.mf
applet.policy
build.xml
I want to know how can i acces a data.txt file in packageTWO(when i run Block_Breaker through a jar file and not netbeans). Normally if run through netbeans the following code will work:
FileWriter x=new FileWriter("src/packageTWO/data.txt");
PrintWriter pr=new PrintWriter(x);
But if i run a jar file that netbeans created it doesnt work.
You can't write to that file once it is packaged into a jar file.
Yet reading is still possible using one of the following:
<YourClass>.class.getClassLoader().getResourceAsStream("packageTWO/data.txt");
// or
this.getClass().getResourceAsStream("/packageTWO/data.txt");
witch gives you an InputStream witch you can use to retrieve the content of the file.
If you are required to wite to that file then the simplest way is not to pack it into the jar but have it standalone some where on the filesystem.
More infos about getResourceAstream in the javadoc
This is because your .jar file does not include a folder named src/
Please use ClassLoader.getResource to load resources.

finding static file in jar

I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored a csv file in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource(ApplicationConstants.ALIASESFILE).getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, it gives a path as shown below
D:\javaProjects\AutomodeGS_Prachi\lib\internal\RESTWSGS.jar!\aliases\aliases.csv
I am getting a file notfound exception.What could be wrong?
The getResource() method is returning a "jar:" URL. The path component of that URL is not a normal filesystem pathname, and can't be opened directly using Java's file classes.
The simple way to do this is to use Class.getResourceAsStream(...) to open the stream. If you need an "identifier" for the JAR entry, use Class.getResource(...), but then open the stream using URL.openStream().
This works fine from glassfish may be because glassfish has exploded jar on file system so that your csv file is acutually a file to the file system,
if you try to read it from another project it fails because the jar containing your file is in classpath that is fine, but the csv file is under jar file and it is no longer a File
You can read it as Stream
InputStream is = MyClass.class.getResourceAsStream(ApplicationConstants.ALIASESFILE);

Categories