简体   繁体   English

具有简单Web服务器的Java应用程序已导出为JAR

[英]Java application with simple Web Server exported as JAR

I have created a simple Web Server application using Java APIs. 我已经使用Java API创建了一个简单的Web服务器应用程序。
Web server is working as expected and I am able to get the HTML pages in the browser. Web服务器按预期方式工作,并且能够在浏览器中获取HTML页面。

I am developing this Java App on a Windows Machine. 我正在Windows计算机上开发此Java App。 For testing I am exporting my app as JAR and then testing it on Target Device which is a Linux Box. 为了进行测试,我将应用程序导出为JAR,然后在目标设备(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. 在我的应用中,我创建了一个“ webroot”文件夹,并存储了该Web服务器需要服务的所有HTML文件。

So when I create JAR file of the app then it has "webroot" folder with all the html files in it. 因此,当我创建应用程序的JAR文件时,它将具有“ webroot”文件夹,其中包含所有html文件。 When I run this application on Windows then I am able to get the html pages. 当我在Windows上运行此应用程序时,便可以获取html页面。 But when I run this application on Linux box as a JAR then I am not able to retrieve html files. 但是,当我在Linux上以JAR格式运行该应用程序时,则无法检索html文件。 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. 同样,当我在JAR之外复制“ webroot”文件夹时,它也起作用了,并且能够看到HTML页面在浏览器中传递。

So is there any way I can access html pages which are in the jar file itself without copying them outside? 那么,有什么方法可以访问jar文件本身中的html页面,而无需将其复制到外部?

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. 因此,上面的jar文件具有webroot文件夹。 And from the code I am accessing it as "webroot/FileName.html". 从代码中,我将其访问为“ webroot / FileName.html”。 If I keep webroot as in the same folder as jar then it does work. 如果我将webroot保留在与jar相同的文件夹中,那么它将起作用。

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. 在不了解您实际如何提供内容的情况下,很难说出您做错了什么,但是您始终可以使用Class.getResourceAsStream()从类路径访问资源。 If you're running it as a jar file, then the contents of the jar file are included in the classpath too. 如果您将其作为jar文件运行,那么jar文件的内容也将包含在类路径中。

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. 要在jar文件之外提供内容,请在类路径中包含“ webroot”,或创建某种机制来首先尝试类路径,然后再尝试外部路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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