简体   繁体   中英

Java application with simple Web Server exported as JAR

I have created a simple Web Server application using Java APIs.
Web server is working as expected and I am able to get the HTML pages in the browser.

I am developing this Java App on a Windows Machine. For testing I am exporting my app as JAR and then testing it on Target Device which is a Linux Box. In my app I have created a "webroot" folder and I am storing all the HTML files, that web server needs to serve.

So when I create JAR file of the app then it has "webroot" folder with all the html files in it. When I run this application on Windows then I am able to get the html pages. But when I run this application on Linux box as a JAR then I am not able to retrieve html files. Also when I copy my "webroot" folder outside the JAR then it works and I am able to see the HTML pages getting delivered in the browser.

So is there any way I can access html pages which are in the jar file itself without copying them outside?

My Project Folder structure is as below:

/src
  -com.myprj.server -> contains server Java files 
/webroot -> all the html pages
/bin -> jar files as per the above package path 
/myprj.jar -> Project jar file 

So above jar file has the webroot folder. And from the code I am accessing it as "webroot/FileName.html". If I keep webroot as in the same folder as jar then it does work.

Without seeing how you're actually serving the content, it's hard to say what you're doing wrong, but you can always use Class.getResourceAsStream() to access the resources from the classpath. If you're running it as a jar file, then the contents of the jar file are included in the classpath too.

To serve content outside of the jar file, either include your "webroot" in the classpath, or create some kind of mechanism to first try the classpath and then an outside path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM