Problems running react js in java spring mvc - java

I'm learning to use React and wanted to use it in a SpringBoot application, but I just can't seem to get this working.
I can create a website using Spring-MVC and also execute JavaScript code on it (without React, JSX). But I just can't figure out how to use JavaScript code that uses React.
What I got working so far is...
A website with a Spring-MVC backend
Executing javascript code on this website
Building a jar of the application using maven
Creating a React project inside my spring project using the create-react-app tool
Compiling the React code using a maven plugin (following this tutorial) (or at least I think it's working, because maven says that the build is successfull)
Loading the React js script in the browser
What is not working is...
Executing the React js script in the browser or debugging the script
The simple React code I want to execute looks like this: test.js
import React from "react";
import ReactDOM from "react-dom";
export default function TestComponent() {
return <div>
<p>Hello There!</p>
</div>;
}
debugger;
ReactDOM.render(<TestComponent />, document.getElementById('hello_there'));
alert("Hello There!");
It should just add some text into the html file to show it's working, but it doesn't. The html (thymeleaf) file looks like this: home.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Go Server - JFabricationGames</title>
</head>
<body>
<h1>JFG - Go Server (WIP)</h1>
<img th:src="#{images/welcome.png}" width=500 />
<br>
<a th:href="#{/login}">Login</a>
<div id="hello_there"></div>
<div id="hello_there_2"></div>
<script src="test.js"></script>
<!-- <script src="test_no_react.js"></script> -->
</body>
</html>
And my Spring project looks like this:
and is build using this pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>net.jfabricationgames</groupId>
<artifactId>go_server_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>go_server_spring</name>
<description>A Go Server</description>
<properties>
<java.version>1.8</java.version>
<frontend-src-dir>${project.basedir}/src/main/app</frontend-src-dir>
<node.version>v12.3.1</node.version>
<yarn.version>v1.16.0</yarn.version>
<frontend-maven-plugin.version>1.7.6</frontend-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<nodeVersion>${node.version}</nodeVersion>
<yarnVersion>${yarn.version}</yarnVersion>
<workingDirectory>${frontend-src-dir}</workingDirectory>
<installDirectory>${project.build.directory}</installDirectory>
</configuration>
<executions>
<execution>
<id>install-frontend-tools</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
</execution>
<execution>
<id>yarn-install</id>
<goals>
<goal>yarn</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>build-frontend</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>position-react-build</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<outputDirectory>${project.build.outputDirectory}/static</outputDirectory>
<resources>
<resource>
<directory>${frontend-src-dir}/build</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If you want to have a look at the full project code you can find it on GitHub: https://github.com/tfassbender/go_server_spring/tree/react_test_2
Now when opening the website in the browser (firefox 72.0.2 (64-Bit)) and using the developer tools I can see that the script was loaded and I can even see the script code:
But what is strange is that I can't seem to debugg the file or even find the file in the debugger...
... although it is loaded and even seems to be executed, because the developer tools show an error in the first line of the script (in the image above).
And what is also kind of strange to me is that the script (test.js) that I can see in the browser (when opening it from the network tab of the developer tools) shows exactly the react code from the test.js file. I would expect it to be compiled to ES5, but I'm very new to React so I don't realy know if this is a problem or an expected behaviour.
So basically the problem is that I can't debugg my javascript/React code and I'm not shure whether I load the correct file or even compiled it correctly (because I'm realy new to React). In fact I have no idea what's going on here and what the problem might be.
So any help would be great.

The issue here seems to be that you want to run the javascript output by create-react-app on the build, but actually, you're just running a static file test.js which is in the src/main/app/public directory.
This test.js file is not touched by create-react-app at all, so you're right that it's not being compiled to ES5, it's simply being served statically as it is. The error you're seeing in Firefox is exactly because of that - it can't handle the import statements (fixing this directly is a separate issue which I won't get into here, but essentially you won't have this problem with the create-react-app compiled code because it'll be bundled and transpiled to ES5).
Only the code with the entry point at src/main/app/src/index.js will be compiled by create-react-app and indeed it is, and then as per your pom.xml configuration is then copied into target/classes/static/static/{bundle files}.js.
What you want to do is link to those create-react-app generated bundle files, instead of this test.js file. Whatever React code those built bundles contain will run just fine. It's unclear what the role of test.js is here, but to conclude, it's simply not being compiled at all and that's the cause of the error, nothing to do with React per se. You should just delete it and link instead to your create-react-app generated bundles.

Related

How to build standalone executable cucumber jar file?

I want to create a executable jar file so anyone can run it from their computer with least install require components.
I found several tutorial but none of them a work.
When I execute jar file I've built they are return error like:
Error: Could not find or load main class fully.qualified.MainClass
Caused by: java.lang.ClassNotFoundException: fully.qualified.MainClass
OR like this:
Error: Could not find or load main class io.cucumber.core.cli.Main
Caused by: java.lang.ClassNotFoundException: io.cucumber.core.cli.Main
My project run from Intellij with no problem.
Here my project structure
https://i.stack.imgur.com/NcQzf.png
And my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>CucumberSelenium</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.version>7.6.0</cucumber.version>
<selenium.version>4.8.0</selenium.version>
<webdrivermanager.version>5.2.1</webdrivermanager.version>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
<apache.common.version>2.4</apache.common.version>
<projectlombok.version>1.18.24</projectlombok.version>
<maven.compiler.plugin.version>3.10.1</maven.compiler.plugin.version>
<maven.surefire.plugin.version>3.0.0-M7</maven.surefire.plugin.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>${cucumber.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.jupiter.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- JUnit Platform -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<!-- Web Driver Manager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
<!-- Apache Common -->
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>${apache.common.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>19</source>
<target>19</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
<debugForkedProcess>true</debugForkedProcess>
<forkCount>0</forkCount>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<mainClass>io.cucumber.core.cli.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I've run mvn clean compile assembly:single, it's output a CucumberSelenium-1.0-SNAPSHOT-jar-with-dependencies.jar file, but it wont runs.
I just came across a similar issue and, despite trying the ubiquitous
java -cp . org.example.Main
(while on current directory being where the Main.class is), I kept getting this dreaded
"Error: Could not find or load main class"
I eventually resorted to comparing the actual command with parameters invoked by IntelliJ (on IntelliJ IDEA's debug log) with mine, and discovered that the following solves the issue:
java -cp C:\Users\WebViwer\IdeaProjects\MyProj\target\classes org.example.Main
I am guessing that once the fully qualified class name is specified, the current directory (where the class resides) is no longer valid as a classpath: Only the top level classes directory should be specified (in this org.example, 2 levels up).
You have a a few problems going on.
The maven-assembly-plugin should not be a in the dependencies section. It is not a dependency used by the runtime code of your project.
Your step definitions, feature files and glue code located in src/test are not included in the jar file build by the assembly plugin.
Your test scoped dependencies will also not be included by the assembly plugin either.
You must configure the containerDescriptorHandler of the assembly plugin with metaInf-services or the plugin will not merge files in META-INF/services correctly.
You can verify most of these by opening the generated jar file (it's a .zip file in disguise).
My project run from Intellij with no problem.
When running tests in Intelij you are using the test scope, the jar file only includes runtime scoped code.
For a more comparable test you must create a new run configuration that invokes the main method.
I found several tutorial but none of them a work.
It appears that you are relatively new to Java and Maven. It would be prudent to follow a proper course first rather than tutorials. You are missing fundamental knowledge that is generally not taught in tutorials.
I want to create a executable jar file so anyone can run it from their computer with least install require components.
It's also prudent to consider why you are doing this.
Tests will typically change as quickly as the source code they are testing does. Manually distribution of jar files won't keep up with this.
This means it's generally better to integrate the tests and the test source code into the automated build pipeline of the project they test.
This also means that you don't need to distribute your tests as a jar. Rather you should expect (and possibly train) people to use Maven, GIT and Java.

Deployment of basic Java web application to AWS : javax/xml/bind/JAXBException

I keep getting this error while trying to deploy an application to AWS:
An internal error occurred during: "Updating AWS Elastic Beanstalk environment:
SampleWebApplication".
javax/xml/bind/JAXBException
Here is what I do:
I create new Maven project based on maven-archetype-webapp 1.0
I configure the pom.xml file with dependencies (full file below)
I type in whatever to index.jsp (it's supposed to be super easy application)
I run it on tomcat7:run, it works like a charm on http://localhost:8080/
I create AWS Server
I select the project, I choose Amazon Web Services Tool --> Deploy to AWS Elastik Beanstalk, choose the added server and I keep getting this message:
I am not able to find any information about this error in the internet. The only thing that I have found is that it is connected to Java version, but I am running Java 1.8 (as was suggested in one post that I found).
Can anyone please help me? I am following this instruction for deployment of the application.
I'm super new to AWS so I don't even know where to start!
index.jsp
<html>
<body>
<h2>Hello There!</h2>
</body>
</html>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dominikazb</groupId>
<artifactId>SampleWebApplication</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleWebApplication Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<tomcat.version>7.0.50</tomcat.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>SampleWebApplication</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<wtpversion>2.0</wtpversion>
<wtpContextName>todo</wtpContextName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webappDirectory>${project.build.directory}/${project.artifactId}
</webappDirectory>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
</plugins>
</build>
</project>
Project structure
Please, please, please help!
Finally, I find an answer to this problem in the website:
https://github.com/aws/aws-toolkit-eclipse/issues/123
The examples there were in Unix. Mine is in Window 7. What I did base on the website suggestion is:
Find where is the file jaxb-api-2.2.5.jar located. I am not sure if version 2.2.5 is a must. Suggest try whatever you have.
Mine is located in
C:\Users\myUserName\.m2\repository\javax\xml\bind\jaxb-api\2.2.5.
Exit the Eclipse IDE.
Open PowerShell in Admin mode.
cd to your user directory (my case C:\Users\myUserName) and execute the following command to open the Eclipse IDE with a -dev option which points to the jaxb-api-2.2.5.jar.
C:\Users\myUserName\eclipse\jee-2020-09\eclipse\eclipse -dev $(ls ~/.m2/repository/javax/xml/bind/jaxb-api/*/*[0-9].jar | Select-Object -Last 1)
Certainly, the location of your eclipse.exe can be different.
Happy coding!

Error: Could not find or load main class Maven Spring Boot Application - Executable JAR

I am trying to run an executable jar but got the following message :
Could not find or load main class
Here is the content of MANIFEST.MF file:
Manifest-Version: 1.0
Created-By: Apache Maven Built-By: Administrator
Build-Jdk: 11.0.3
Class-Path: spring-boot-starter-batch-2.2.0.RELEASE.jar spring-boot-starter-1.5.6.RELEASE.jar spring-boot-1.5.6.RELEASE.jar spring-boot-autocon
figure-1.5.6.RELEASE.jar .......
Main-Class: EAB.ActiveTeam.Exporter.App
I am really stuck as I searched the web, tried many solutions to make my executable JAR run!
Please help, I sent over 5 hours trying to fix with no luck!
here is my POM.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>EAB.ActiveTeam.Exporter</groupId>
<artifactId>EAB.ActiveTeam.Exporter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EAB.ActiveTeam.Exporter</name>
<description>EAB.ActiveTeam.Exporter</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>EAB.ActiveTeam.Exporter.App</mainClass>
<layout>jar</layout>
<outputDirectory>../libs</outputDirectory>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When it comes to spring boot application you can't create a regular jar file, because spring boot application jar is not a jar at all. It doesn't have an internal directory layout of jar (all dependencies are in BOOT-INF/lib for example) and there are some other subtle differences.
So I believe the issue is with maven here.
Instead of trying to create jar "by youself" and add all the dependencies there you should use spring boot maven plugin:
It "knows" how to create a spring boot jar properly.
By default it will also resolve your "main" file (the one with method main and #SpringBootApplication annotation) however you can specify it in plugin configuration to be on the safe side.
Then you'll be able to the project with :
java -jar <Name_of_your_jar>

How to load my localhost java spring boot web app from an executable file

I have a spring boot web application in Java and which uses front end libraries/framework like Angular/React. Suppose my web app URL is http://localhost:8080/xyz. I need to create an executable file(platform independent) and when I click on it then it should open my web app in browser window and the application should start. Can someone tell how can I achieve this.
EDIT : I just want something clickable on my desktop and upon clicking on it will run the application and then it will open the browser window which will load the web app url and user can hit/do rest of things as a normal web app.So the clickable icon should function as a desktop app but functionality is to run the java web app and open it in browser.
Adding pom.xml here , which i used in my app.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>proman</artifactId>
<groupId>com.upgrad.proman</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>proman-api</artifactId>
<dependencies>
<dependency>
<groupId>com.upgrad.proman</groupId>
<artifactId>proman-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<output>${project.build.directory}/generated-sources</output>
<language>spring</language>
<library>spring-boot</library>
<generateApis>false</generateApis>
<generateModels>true</generateModels>
<modelPackage>com.upgrad.proman.api.model</modelPackage>
<configOptions>
<java8>true</java8>
<sourceFolder>.</sourceFolder>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.18</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-generators</artifactId>
<version>1.0.0-rc0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>signup</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/endpoints/signup.json</inputSpec>
<language>spring</language>
</configuration>
</execution>
<execution>
<id>useradmin</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/endpoints/useradmin.json</inputSpec>
<language>spring</language>
</configuration>
</execution>
<execution>
<id>authentication</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/endpoints/authentication.json</inputSpec>
<language>spring</language>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Make the app runnable:
<build>
<defaultGoal>clean package</defaultGoal>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
Update the starter class (be sure to check headless(false)):
#SpringBootApplication
public class DesktopBootApplication {
public static void main(String[] args) {
// check for availability:
if (!Desktop.isDesktopSupported()) {
System.out.println("This app needs a desktop manager to run, exiting.");
System.exit(1);
}
new SpringApplicationBuilder(DesktopBootApplication.class).headless(false).run(args);
}
#EventListener(ApplicationReadyEvent.class)
public void openBrowserAfterStartup() throws IOException, URISyntaxException {
// open default browser after start:
Desktop.getDesktop().browse(new URI("http://localhost:8080"));
}
}
You might want to add a shutdown option to stop the server. I.e. using the Actuator shutdown resource, you can stop it from JavaScript in your page.
I used a tray icon here:
#Configuration
public class DesktopConfiguration {
#Autowired
private ApplicationContext appContext;
// Add a tray icon to stop the app:
#Bean
public void openTrayIcon() throws Exception {
TrayIcon icon = new TrayIcon(new ImageIcon(this.getClass().getResource("/spring.png")).getImage());
icon.setImageAutoSize(true);
icon.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting app ...");
SystemTray.getSystemTray().remove(icon);
SpringApplication.exit(appContext);
}
});
SystemTray.getSystemTray().add(icon);
icon.displayMessage("Spring Boot", "Application started", MessageType.INFO);
}
}
You need some kind of icon to get this to work. Save any png file (called spring.png) in src/main/resources.
Build the app running mvnw. It can be found in the target directory afterwards.
You can add the command
java -jar "location-of-your-jar"
in a batch file and click it.
The below can be saved as some-file.bat and will run in background on a click and you will have to kill it manually as per your requirements. If you want to stop application on close of the terminal, comment the lines with ("rem") and uncomment below lines.
#echo off
setlocal
rem rem if JAVA is set and run from :startapp labeled section below, else the program exit through :end labeled section.
if not "[%JAVA_HOME%]"=="[]" goto start_app
echo. JAVA_HOME not set. Application will not run!
goto end
:start_app
echo. Using java in %JAVA_HOME%
rem run java application in background and you will have to manually kill the process to stop the app(not recommended).
start javaw -jar "/your/location/myapp.jar"
rem comment above line and uncomment below to run java application in foreground so that you can close the terminal and app will close (recommended).
rem java -jar "/your/location/myapp.jar"
echo. Your spring boot app is started...
goto end
:end
rem clean if files are created.
pause
Your pom file should be like below
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.sample</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
...........
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Make sure that your project pom.xml has parent and build as above
Then run maven command
mvn package
to build jar file.
You can run jar file in any environment by executing below command
java -jar <filename.jar>
or
mvn spring-boot:run
from your root path of your project
Another option is configure your application to start as service [1][2].
The install program will:
Copy app files
Create service
Start service
Create a desktop icon pointing to app's url
[1] https://dzone.com/articles/spring-boot-as-a-windows-service-in-5-minutes
[2] https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

NetBeans using maven project will not step through the code when attaching to a jvm in another application

My first time using NetBeans with Maven. Everything works great from within NetBeans. Can build, package install, etc. Can debug and step through code. The problem starts when I attempt to debug my code by attaching to another application which links a JVM instance instead of debugging within NetBeans.
I am attaching to a process using shared memory transport and it succeeds. When I call from that process a java method with a breakpoint, it breaks. I can inspect variables and I can tell that it stopped in the right place, but it refuses to step through the code. NetBeans does not highlite in green the line where it stopped and stepping through the code leads netbeans into some Java bowels popping up Integer class, HashMap class, etc. and complaining that no source code is available. I can however set another breakpoint in my code and hit continue. It will stop there, but again will not step.
My speculation is that it has something to do with maven and incomplete debug information in my class files. If I reconfigure my project to use ant everything works correctly, but going back to maven screws it up again.
The Debugging view in NetBeans shows "Hidden Source Calls":
Anybody has any thoughts? Thanks
I am starting my application with linked jvm.dll with the following options:
-Xdebug -Xrunjdwp:transport=dt_shmem,address=myexecutable,server=y,suspend=n
Here are my sample pom files:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.some.company.edited</groupId>
<artifactId>atpg-parent</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<name>Parent Project</name>
<properties>
<!-- Build Metadata -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- App & Platform Versions -->
<java.target.version>1.7</java.target.version>
<junit.version>4.12</junit.version>
<!-- Build Plugin Versions -->
<maven.compiler.plugin>3.2</maven.compiler.plugin>
</properties>
<modules>
<module>atpg-main</module>
<module>atpg-dm</module>
</modules>
<!-- Dependency VERSIONS -->
<dependencyManagement>
<dependencies>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Shared Dependencies -->
<dependencies>
<!-- Test Dependencies (can't hurt to include these all the time) -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>${maven.compiler.plugin}</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.target.version}</source>
<target>${java.target.version}</target>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
</plugin>
</plugins>
</build>
http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
<parent>
<groupId>com.some.company.edited</groupId>
<artifactId>atpg-parent</artifactId>
<version>0.1</version>
</parent>
<artifactId>atpg-main</artifactId>
<packaging>jar</packaging>
<name>Main Project</name>
<dependencies>
<!-- Project module dependencies -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>atpg-dm</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
<parent>
<groupId>com.some.company.edited</groupId>
<artifactId>atpg-parent</artifactId>
<version>0.1</version>
</parent>
<artifactId>atpg-dm</artifactId>
<packaging>jar</packaging>
<name>Exchange Format Data Model</name>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<version>0.13.1</version>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Issue resolved. Resolution was extremely simple. Turns out that with Maven projects created outside of the NetBeans environment one has to open not only the parent project, but also all the modules in the project. Due to my ignorance, I did not do that. I only opened the parent project.
In the open project dialog there is actually a check box "Open Required Projects:" and it was off. Once I checked it, my parent opened with all modules as separate projects and everything works beautifully well.
So, the problem had nothing to do with incomplete debug information in Java class files. It was just a NetBeans peculiarity that this check box is off by default.

Categories