Karthick Gandhi
Karthick Gandhi

Reputation: 1

How to create a HTML file inside a .war file using Java?

I am trying to create a HTML file dynamically inside a .war file deployed in the JBoss server.

It doesn't allow me to create a file.

Can someone help me out to create a HTML file inside a deployed .war file in Java?

Upvotes: 0

Views: 828

Answers (4)

Roman C
Roman C

Reputation: 1

In that case the war file should be deployed as exploded. Use

String jbossHome = System.getenv("JBOSS_HOME");

to access to the server deployment.

Then you need a file separator

String separator = System.getProperty("file.separator");

Then construct the path to the deployment root. Assume you have default

String deployRootPath = jbossHome + separator + "server" + separator + "default" + separator + "deploy" + separator;
File dir = new File(deployRootPath + "mywebapp.war");
if (dir.exists() && dir.isDirectory()) {
   File myHtmlFile = new File(dir+separator+"myhtmlfile.html"); 
   myHtmlFile.createNewFile();

Upvotes: 1

pstanton
pstanton

Reputation: 36640

Behind the scenes, A .war file is just a .zip file.

I'm not going to go into how your container (JBOSS) will react when you modify the contents of a war file (probably reload your webapp automatically by default?) but you can treat the .war as any other .zip except with a different file extension.

see the other posts about how to work with zip files in java if that's what you need to do.

Upvotes: 0

Akhil K Nambiar
Akhil K Nambiar

Reputation: 3975

WAR is somewhat a ZIP file renamed. Uncompress add file. Rezip. I guess some library will be there to allow us to add files without completely unzipping. Just google for that.

Upvotes: 0

Peter
Peter

Reputation: 5164

A WAR file is a simple ZIP file. I don't know if there is a way to "mount" a zip file and access it like a file system but you could definitely unzip the WAR file, add you file and zip it again.

Adding files to zipfile

Upvotes: 0

Related Questions