java - 如何从可执行文件加载我的本地主机 java spring boot web 应用程序

标签 java spring-boot desktop-application

我有一个 Java 中的 Spring Boot Web 应用程序,它使用像 Angular/React 这样的前端库/框架。假设我的网络应用程序 URL 是 http://localhost:8080/xyz 。我需要创建一个可执行文件(独立于平台),当我单击它时,它应该在浏览器窗口中打开我的网络应用程序,并且应用程序应该启动。有人能告诉我如何实现这一目标吗?

编辑: 我只想要桌面上可点击的内容,点击它后将运行该应用程序,然后它将打开浏览器窗口,该窗口将加载网络应用程序网址,用户可以像普通 Web 应用程序一样点击/执行其余操作。因此,可单击图标应该充当桌面应用程序,但功能是运行 Java Web 应用程序并在浏览器中打开它。

在此处添加我在应用程序中使用的 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">
    <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>

最佳答案

使应用程序可运行:

<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>

更新起始类(请务必检查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"));
    }
}

您可能想要添加关闭选项来停止服务器。 IE。使用Actuator shutdown resource ,您可以从页面中的 JavaScript 停止它。

我在这里使用了托盘图标:

@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);
    }
}

您需要某种图标才能使其正常工作。将任何 png 文件(名为 spring.png)保存在 src/main/resources 中。

构建运行mvnw的应用。之后可以在target目录中找到它。

关于java - 如何从可执行文件加载我的本地主机 java spring boot web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54683439/

相关文章:

node.js - 如何使用快捷键打开Electron App?

java - Spring Boot 桌面/CLI 应用程序中组件的正确@Scope 是什么?

java - 并发 hashmap 缓存中的空值

java - Spring boot 1.2.5.RELEASE 与 OpenShift Tomcat 7 不兼容

spring-boot - 运行 springboot oauth2 应用程序时出错 : java. security.PrivilegedActionException: null

java - JUnit 5对没有SpringBootApplication的子项目进行测试

.net - 将 MVC 应用程序作为独立应用程序运行

java - Hazelcast 分区计数和线程并发

java - 动态 HTML 表行已添加、填充并插入到数据库中

java - 如何在 FXML 中定义 Stage?