【问题标题】:How to load my localhost java spring boot web app from an executable file如何从可执行文件加载我的本地主机 java spring boot web 应用程序
【发布时间】:2019-02-14 04:57:32
【问题描述】:

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

编辑: 我只想在我的桌面上点击一些东西,点击它会运行应用程序,然后它会打开浏览器窗口,该窗口将加载网络应用程序的 url,用户可以像普通的网络应用一样点击/做其余的事情。所以可点击的图标应该作为桌面应用运行,但功能是运行 Java 网络应用并在浏览器中打开它。

在此处添加 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>

【问题讨论】:

  • 构建您的 spring-boot 应用程序,然后您可以通过执行 "java -jar " 在任何环境中运行 jar
  • 所以,你的意思是,如果我执行“java -jar ”,那么它将创建一个可执行文件。你也可以告诉我是否需要修改我的 pom.xml 文件来实现这一点。
  • 我不明白谁以及为什么不赞成这个问题。
  • “我需要创建一个可执行文件(平台无关)”,这个可执行文件是输出 jar :-)

标签: java spring-boot desktop-application


【解决方案1】:

使应用程序可运行:

<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目录中找到。

【讨论】:

  • 我修改了我的 pom.xml(如问题中给出的)文件以使应用程序可运行,如您所述,当我尝试构建时出现错误 - “目标 org.springframework.boot 的执行默认值:spring-boot-maven-plugin:2.1.2.RELEASE:repackage failed:"
  • 重新打包失败后怎么办?您是否将packaging 设置为jar
  • 我已经修复了错误,谢谢。但我所期待的是 - 我应该在桌面上有一个可点击的图标,点击它会运行应用程序,然后只有它应该打开浏览器。抱歉,我错过了在我的实际问题中提及的一部分。我已经编辑过了。所以可点击的图标应该作为桌面应用程序,但功能是运行 java web 应用程序并在浏览器中打开它。
【解决方案2】:

你可以添加命令 java -jar "location-of-your-jar" 在批处理文件中并单击它。

下面的内容可以保存为 some-file.bat 并在点击后在后台运行,您必须根据您的要求手动杀死它。如果您想在终端关闭时停止应用程序,请使用 ("rem") 注释这些行并取消注释下面的行。

@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

【讨论】:

    【解决方案3】:

    你的 pom 文件应该如下所示

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

    确保你的项目 pom.xml 有 parentbuild 如上所述

    然后运行maven命令

    mvn package
    

    构建jar文件。

    您可以通过执行以下命令在任何环境中运行 jar 文件

    java -jar <filename.jar>
    

    mvn spring-boot:run
    

    从项目的根路径

    【讨论】:

    • 我已经编辑了我的问题以添加这个 - “编辑:我只想在我的桌面上点击一些东西,点击它会打开浏览器窗口,该窗口将加载网络应用程序 url,用户可以点击/其余的事情就像一个普通的网络应用程序一样。” .我期待这个。
    • @user3742125,检查我的答案是否是你要找的。让我知道它是否适合您..
    【解决方案4】:

    另一个选项是将您的应用程序配置为作为服务启动 [1][2]。

    安装程序将:

    1. 复制应用文件
    2. 创建服务
    3. 启动服务
    4. 创建一个指向应用程序 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

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      相关资源
      最近更新 更多