Load Tomcat into Java Desktop application

Asked

Viewed 210 times

0

After a lot of pain, I was able to develop a Java application, with graphical interface (swing), which simulates access to a particular site and extracts data. The problem is that as it simulates a browser (with Htmlunit), it needs Tomcat and when I try to run the application on another machine (and I will need to use it on both Windows and MAC machines), it does not run because of the Tomcat radius.

The question is: How do I ship Tomcat into an application that is not Web, so that the jar is "self-sufficient"? I use Netbeans, and if information is important, the application uses Apache Commons Codec, Apache POI (Apache) and Htmlunit libraries.

  • Maybe it’s worth it, instead of Tomcat, you take a look at Jetty.

  • I am accepting anything that solves my problem. The question is: How do I ship the web server into my Java application??

3 answers

0

I thought of 2 possible solutions:

1st Solution

A) In the Tomcat project, you make the web application, with all the requests you want. This project will take the data you want and play in a file or database.

B) Your Desktop project queries this database for this information.

--

2° Solution

It would be you to make a request to the site by the java itself. Through Urlconnection. Take a look at this link: https://www.javaworld.com/article/2075162/core-java/dodge-the-traps-hiding-in-the-urlconnection-class.html

  • I took a look at Urlconnection, but I didn’t see how to use it in my project, because the site in question, to be accessed, is kind of complex. It "presents itself" as Get, but the login form is POST (and the login is fundamental). Not only that, it has a second login screen (2-step authentication) which, in fact, is a frame of this screen, which, to be accessed, it is necessary to read the HTML of the page, take a token and build the lkink that leads to the frame, to then press the buttons that guarantee authentication (no, this site was not meant to be automatically accessed by an application).

  • With Htmlunit I can simulate hi browser and read the HTML of the pages and interact with the elements, but it demands Tomcat. I can do this only with Urlconnection (or, even if I use other libraries, without Tomcat)?

  • Vinicius, how about using the first option then? so you would have a better control

  • So... the problem is that there is no web application in the project. It is the application that accesses the site, simulating a browser. In addition to that, for this, I would have to ship the server and the database, and other features that the application already uses... It would be too heavy for the need in question

0

For this case, recommend you use the embedded Jetty, you can start the server along with the start of the application. You just need to set up and understand a few things, if you have no difficulty with English, I recommend taking a look at this site that has an example of server embedded with Jetty.

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

0

I already faced this problem, my solution was to use framewok:
http://sparkjava.com/

He uses Jetty as a server. And he’s very quiet. In my case I generated a . jar of my project, to start on the customer’s machine it was necessary only to run the command :

java -jar meuprojeto.jar

However, I am realizing q vc already has a whole application ready. Vc could generate an . exe(for windows) using the http://www.jrsoftware.org/isinfo.php, logic would be the following:

  1. You select the files q he has q copy to the client machine. You compile your project(.War) on a Tomcat.At the time of installing your executable, it will copy this Tomcat to your client.

  2. You can also tell the command q it should run, when you finish copying the project to an installation folder. That is, it will start your Tomcat.

  3. Be careful with the port q vc will start your Tomcat on your client’s machine. Because it may be in use.


Another solution would also be to use spring-boot, it already comes with an embedded Tomcat. You would compile your project normally, in the end it generates a . jar and vc would run the project as follows :

 java -jar seuprojeto.jar 
  • I’m trying spring-boot but it seems like it only makes web application... is this msm? If it is, is not the case, My application has to be msm desktop...

  • It generate a .jar. You can run your application "java -jar your project.jar", just q take care of the q port it will run.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.