1

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. Can someone help me out to create a HTML file inside a deployed .war file in Java?

Roman C
  • 49,761
  • 33
  • 66
  • 176

4 Answers4

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();
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

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

Community
  • 1
  • 1
Peter
  • 4,752
  • 2
  • 20
  • 32
0

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.

Akhil K Nambiar
  • 3,835
  • 13
  • 47
  • 85
0

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.

Community
  • 1
  • 1
pstanton
  • 35,033
  • 24
  • 126
  • 168