Why can I run my app VSCode but not correctly in CLI?, its a spring boot application

youzine :

I'm building a web application in spring boot.

The development environment is Gradle and I am using visual studio code.

I'm using visual studio code's debug function, spring boot dashboard, and when I start the application, the page is displayed correctly, but when I make the web application a jar file and run it with java -jar, it doesn't work with some page transitions.

Please tell me what I should do to make it work and what is happening. (I think that spring boot can't find the some path, but I don't know what is and how to fix it.)

Here's the page I was on when I accessed 「localhost:8080/gallery」

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Sep 01 23:09:16 JST 2020
There was an unexpected error (type=Internal Server Error, status=500).

The following is the error I get when I use java -jar to access a specific gallery page.

2020-09-01 23:09:14.129  INFO 15464 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-09-01 23:09:14.143  INFO 15464 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 14 ms
Check image file path
2020-09-01 23:09:16.656 ERROR 15464 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
        at com.example.sweepea.GetImage.getImage(GetImage.java:23) ~[classes!/:na]
        at com.example.sweepea.swepeaController.getGallery(swepeaController.java:21) ~[classes!/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]

The methods in the controller class are as follows. 「index」and「about」is work.

    @GetMapping("/index")
    public String getIndex(){
        return "index";
    }

    @GetMapping("/gallery")
    public String getGallery(Model model){
            List<String> imagePath = new GetImage().getImage(new File("src/main/resources/static/images"));
            model.addAttribute("imagePath", imagePath);
            return "gallery";
        }

    @GetMapping("/about")
    public String getAbout(){
            return "about";
        }
}

GetImage class are as follw. It is get .jpg images , add List and return.

@Service
public class GetImage {
    String month = "202009";
    List<String> pathname = new ArrayList<String>();
    
    List<String> getImage(File file){
        File[] filelist = file.listFiles();
        
        

        if(filelist == null){
            System.out.println("Check image file path");
        }

        for (File tmpFile : filelist){
            if(tmpFile.isDirectory()){
                month = tmpFile.getName();
                //System.out.println(month);
                //pathname.add(month);
                getImage(tmpFile);
            
            }else{
                String imagePath = "images/" + month + "/" + tmpFile.getName();
                if(imagePath.substring(imagePath.length() - 3).equals("jpg")){
                    pathname.add(imagePath);
                    //System.out.println(imagePath);
                }
            }

        }
        return pathname;
    }
}

gallery.html

 <body>
        <div id="content">
            <header class="page_header wrapper" >
                <h1><a th:href="@{'/index'}"><img class="logo" th:src="@{images/logo.svg}" alt=""></a></h1>
                <nav class="main_nav">
                    <ul>
                        <ii><a th:href="@{'/index'}">TOP</a></ii>
                        <ii><a th:href="@{'/gallery'}">GALLERY</a></ii>
                        <ii><a th:href="@{'/about'}">ABOUT</a></ii>
                    </ul>
                </nav>
            </header>
            <main>
                <div class="edition">
                    <p>sub title</p>
                </div>
            
            
            
            <h2 class="mounth">Gallery</h2>
            <div class="grid item">
                    <a th:each="image : ${imagePath}" th:href="@{${image}}"><img th:src="@{${image}}" alt=""></a>

            </div>

            
            
            
            </main>
        </body>

source tree is like that


└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── sweepea
    │   │               ├── DemoApplication.java
    │   │               ├── GetImage.java
    │   │               ├── Test.java
    │   │               └── swepeaController.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       │   ├── logo.svg
    │       │   ├── images
    │       │   │   ├── 201910
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 201911
    │       │   │   ├── 201912
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 202001
    │       │   │   ├── 202002
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 202003
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 202004
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 202005
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 202006
    │       │   │   ├── 202007
    │       │   │   ├── 202008
    │       │   │   │   ├── 1.jpg
    │       │   │   ├── 202009
    │       │   │   ├── logo.svg
    │       │   │   └── logo_favicon.png
    │       │   └── style.css
    │       └── templates
    │           ├── about.html
    │           ├── gallery.html
    │           ├── index.html
    │           └── video.html
    └── test
        └── java
            └── com
                └── example
                    └── sweepea
                        └── DemoApplicationTests.java
GreyFairer :

If you package you application in a jar, it's no longer dependent on the source code.

If you start your application using java -jar ... in another folder or on another server, then new File("src/main/resources/static/images") will no longer work.

I think it will work when you execute java -jar ... in the root folder of your project, but that's probably not a long-term solution.

Maven considers the src/main/resources folder as an input folder for files that need to be included in the jar, so after packaging, those files will be available from java as getClass().getResource("/static/images/202008/1.jpg"), and that's how they will be served in Spring Boot (using the ResourceHttpRequestHandler, without the static/ prefix).

If you want to add images later without having to rebuild and repackage your application, it's better to use an absolute server path like "C:\gallery\images" or "/opt/gallery/images".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why the spring boot vaadin app can not run?

How can I correctly run a timed process in my app the will start on stop when the application is in use or in the background?

Why do I get the error "A valid Flask application was not obtained from..." when I use the flask cli to run my app?

Spring-Boot application run configurations with VScode

Why does my Spring Boot web app not run completely in Gradle?

Can I use Spring integration modules in my Spring boot application?

How can I find why system can not run my application?

How can I run a Spring Boot application on port 80

Can I run long tasks in a Spring Boot application?

Why is my Spring application run from my spring boot unit test

Why can't I run custom application on my Beaglebone board?

Why can't I run my WatchOS 2 app?

Why does my spring boot web application fail to load its configuration properties?

ProcessBuilder is not working correctly when I deploy my Spring boot Application to a Linux server

Why I can't find my tables in H2 schema / How can I validate which H2 schema my Spring boot app is working with?

Can not run spring boot application using maven

Can't run Spring Boot Application with docker

How do I annotate my JUnit test so it will run the way my Spring Boot app runs?

How do I configure HikariCP in my Spring Boot app in my application.properties files?

Why App Engine can't connect to Cloud SQL with my Spring Boot app?

Is there a difference between Run As: Spring Boot App and Run As: Java Application?

Why would IntelliJ be able to see my application context, but when I run my tests they can't see it?

I keep getting this @bean error when trying to run my spring boot app with dynamodb and graphql

How to run a Spring Boot maven project in VSCode and how to config the base url of a spring boot web application

(How) Can I run more than one Spring Boot application on the same server and port?

Why I' m Getting Below SpringIntegration Exception while running my application with spring boot

Can I deploy a Spring Boot application on godaddy

Why can CLion correctly build and link Qt, but not run my executable?

My bat file can't run correctly, why?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive