2
votes

Comment charger mon application Web localhost java spring boot à partir d'un fichier exécutable

J'ai une application Web Spring Boot en Java et qui utilise des bibliothèques / framework front-end comme Angular / React. Supposons que l'URL de mon application Web soit http: // localhost: 8080 / xyz . Je dois créer un fichier exécutable (indépendant de la plate-forme) et lorsque je clique dessus, il doit ouvrir mon application Web dans la fenêtre du navigateur et l'application doit démarrer. Quelqu'un peut-il dire comment puis-je y parvenir?

MODIFIER: Je veux juste quelque chose de cliquable sur mon bureau et en cliquant dessus, l'application lancera l'application, puis la fenêtre du navigateur s'ouvre. va charger l'URL de l'application Web et l'utilisateur peut appuyer sur / faire le reste des choses comme une application Web normale.L'icône cliquable doit donc fonctionner comme une application de bureau, mais la fonctionnalité consiste à exécuter l'application Web java et à l'ouvrir dans le navigateur.

Ajout de pom.xml ici, que j'ai utilisé dans mon application.

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


4 commentaires

Créez votre application Spring-Boot, puis vous pouvez exécuter le fichier jar dans n'importe quel environnement en exécutant "java -jar "


Donc, ce que vous dites, c'est que si j'exécute "java -jar ", alors il créera un fichier exécutable. Pouvez-vous également me dire si je dois modifier mon fichier pom.xml pour y parvenir.


Je ne comprends pas qui et pourquoi a voté contre cette question.


"J'ai besoin de créer un fichier exécutable (indépendant de la plateforme)", cet exécutable est le fichier jar de sortie :-)


4 Réponses :


0
votes

Votre fichier pom doit être comme ci-dessous

mvn spring-boot:run

Assurez-vous que votre projet pom.xml a parent et build comme ci-dessus

Ensuite, exécutez la commande maven

java -jar <filename.jar>

pour créer le fichier jar.

Vous pouvez exécuter le fichier jar dans n'importe quel environnement en exécutant la commande ci-dessous

mvn package

ou

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

depuis votre chemin racine de votre projet


2 commentaires

J'avais édité ma question pour ajouter ceci - "EDIT: Je veux juste quelque chose cliquable sur mon bureau et en cliquant dessus ouvrira la fenêtre du navigateur qui chargera l'url de l'application Web et l'utilisateur peut frapper / faire le reste des choses comme un Web normal app. " . J'attends ceci.


@ user3742125, vérifiez si ma réponse correspond à ce que vous recherchez. Faites-moi savoir si cela fonctionne pour vous.



4
votes

Rendre l'application exécutable:

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

Mettez à jour la classe de démarrage (assurez-vous de cocher 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"));
    }
}


3 commentaires

J'ai modifié mon fichier pom.xml (comme indiqué dans la question) pour rendre l'application exécutable comme vous l'avez mentionné et quand j'ai essayé de construire, j'ai eu l'erreur - "Execution default of goal org.springframework.boot: spring-boot-maven-plugin : 2.1.2.RELE‌ ASE: échec du reconditionnement: "


Qu'est-ce qui suit l'échec du reconditionnement? Avez-vous défini packaging sur jar ?


Je l'ai fait fonctionner pour corriger l'erreur, merci. Mais ce à quoi je m'attendais, c'est que je devrais avoir une icône cliquable sur le bureau et en cliquant dessus, l'application lancera l'application, puis elle devrait ouvrir le navigateur. Désolé, j'ai manqué de mentionner une partie de ma question. Je l'ai édité. Ainsi, l'icône cliquable doit fonctionner comme une application de bureau, mais la fonctionnalité consiste à exécuter l'application Web java et à l'ouvrir dans le navigateur.



1
votes

Vous pouvez ajouter la commande java -jar "emplacement-de-votre-pot" dans un fichier batch et cliquez dessus.

Ce qui suit peut être enregistré sous le nom de some-file.bat et s'exécutera en arrière-plan sur un clic et vous devrez le tuer manuellement selon vos besoins. Si vous souhaitez arrêter l'application à la fermeture du terminal, commentez les lignes avec ("rem") et décommentez les lignes ci-dessous.

@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


0 commentaires

0
votes

Une autre option consiste à configurer votre application pour qu'elle démarre en tant que service [1] [2].

Le programme d'installation:

  1. Copier les fichiers d'application
  2. Créer un service
  3. Démarrer le service
  4. Créez une icône de bureau pointant vers l'URL de l'application

    [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


0 commentaires