简体   繁体   English

使用主要方法在浏览器中运行Maven Java应用程序

[英]Run maven java application in browser with main method

I have developed a maven java project. 我已经开发了一个Maven Java项目。 Project has main method and its running as an application. 项目具有主要方法,并且可以作为应用程序运行。 I am using intellijidea as IDE and maven. 我正在使用intellijidea作为IDE和Maven。 What I want to do is, I want to add html files to the project.When I run project as application, I want to display results in browser. 我想做的是,我想向项目中添加html文件。当我以应用程序身份运行项目时,我想在浏览器中显示结果。 How can I do it? 我该怎么做?

this is my main method: 这是我的主要方法:

public class ChemInformatics {

public static void main(String[] args) throws Exception {
    IterativeStreaming streamTest = new IterativeStreaming();
    String path = ChemInformaticsConstants.smallSdfFilePath;
    long startTime = System.nanoTime();

    streamTest.sdfStreamReaderSecondMethod(path);

    long endTime = System.nanoTime();
    streamTest.trackExecutionTime(startTime, endTime);
}

} }

在此处输入图片说明

you need to write a servlet or mvc you can follow the instruction here : https://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm and just write one controller with " return ChemInformatics.main()" 您需要编写servlet或mvc,您可以按照此处的说明进行操作: https ://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm,只需编写一个带有“ return ChemInformatics.main()”的控制器

It's just a brief introduction. 这只是一个简短的介绍。

You have to understand that your Java code need to be interpreted as a Servlet. 您必须了解,您的Java代码需要解释为Servlet。 This is why a lot of answer suggest you to make a "Dynamic Web Project". 这就是为什么很多答案都建议您进行“动态Web项目”的原因。

So you need to run a server, like Tomcat, to deploy a .war of your code on it, and to ensure that your code will be called in your HTML. 因此,您需要运行诸如Tomcat之类的服务器,以在其中部署代码的.war,并确保将在HTML中调用您的代码。 (Your code need to be accesible from somewhere, this is why run it on a server is mandatory) (您的代码需要可以在任何地方访问,这就是为什么必须在服务器上运行它的原因)

If you want to display the result on your HTML page, you should consider using AJAX or create a WebApp. 如果要在HTML页面上显示结果,则应考虑使用AJAX或创建WebApp。 (And of course, an API...) (当然还有一个API ...)

BTW : This is ugly af, not maintenable, and will take a lot of time. 顺便说一句:这是丑陋的事,无法维护,将花费很多时间。

If you still want don't want to use a server 如果您仍然不想使用服务器

You need to buy an applet. 您需要购买一个小程序。 To ensure that an HTML page can call your app, and that's the point, even in local. 确保HTML页面可以调用您的应用,这就是重点,即使在本地也是如此。 I would rather use a GUI. 我宁愿使用GUI。 You can host a servlet with embedded webserver com.sun.net.httpserver.HttpServer or Jetty 您可以使用嵌入式Web服务器com.sun.net.httpserver.HttpServer或Jetty托管servlet。

This question is too OPEN 这个问题太开放了

You need to read some documentations and to add some work you have already tried on the first post if you want a real help. 如果您需要真正的帮助,则需要阅读一些文档并添加一些您已经在第一篇文章中尝试过的工作。

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

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