简体   繁体   English

默认构造函数无法处理隐式超级构造函数抛出的异常类型 FileNotFoundException。 必须定义一个显式构造函数

[英]Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor

I tried FileNotFoundException and try, catch but did not help.我尝试了FileNotFoundException 并尝试捕获但没有帮助。 I think problem is here我认为问题就在这里

InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");

Full class source code:全类源代码:

package uz.xose.webapp;

import java.io.FileInputStream;
import java.io.InputStream;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.cloud.FirestoreClient;



@RestController
public class HelloController { 

    InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");

    GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);

    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(credentials)
        .build();

    FirebaseApp.initializeApp(options);

    Firestore db = FirestoreClient.getFirestore();

     @RequestMapping("/")
     public String index() {
         return "This is the index!\n";
     }

     @RequestMapping("/hello")
     public String index2() {

         return "Hello, World!\n";
     }
}

I am using Spring framework.我正在使用 Spring 框架。

FirebaseAdminSDKJava.json located: src -> main -> resources -> static -> FirebaseAdminSDKJava.json FirebaseAdminSDKJava.json位于:src -> main -> resources -> static -> FirebaseAdminSDKJava.json

"/src/main/resources/static/FirebaseAdminSDKJava.json" is a relative file path. “/src/main/resources/static/FirebaseAdminSDKJava.json”是一个相对文件路径。 Depending on where your application is running, this file may or may not be in that position.根据您的应用程序运行的位置,此文件可能位于也可能不在该位置。 Once built and deployed, the "src" dir won't be there, only in development.构建和部署后,“src”目录将不存在,仅在开发中。

Instead, load this file from the classpath.相反,从类路径加载此文件。 See this answer: How to really read text file from classpath in Java请参阅此答案: How to true read text file from classpath in Java

Classpath includes what you have inside you resources directory.类路径包括您在资源目录中的内容。 please try this请试试这个

1. InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream("FirebaseAdminSDKJava.json");


2 classpath:static/FirebaseAdminSDKJava.json

For debugging, 1. start eclipse in debugger mode or put your code in try block and instead of catching FileNotFoundException catch parent exception "Exception".对于调试, 1. 在调试器模式下启动 eclipse 或将您的代码放在 try 块中,而不是捕获 FileNotFoundException 捕获父异常“异常”。

sample ex样品前

try{
   //your code
}catch(Exception ex){
  ex.printStackTrace();
}

printStackTrace() will show us the actual root cause of the issue printStackTrace() 将向我们展示问题的实际根本原因

hope it will work for you希望它对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 默认构造函数无法处理隐式超级构造函数引发的异常类型IOException。 必须定义一个显式构造函数 - Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor Java错误默认构造函数无法处理隐式超级构造函数引发的异常类型SQLException。 必须定义一个显式构造函数 - Java error Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor 默认构造函数的隐式超级构造函数 getdata() 未定义。 必须定义一个显式构造函数 - Implicit super constructor getdata() is undefined for default constructor. Must define an explicit constructor 隐式超级构造函数AbstractNcssCountRule()未定义为默认构造函数。 必须定义一个显式构造函数 - Implicit super constructor AbstractNcssCountRule() is undefined for default constructor. Must define an explicit constructor 隐式超级构造函数Num()未定义为默认构造函数。必须定义一个显式构造函数,这背后的逻辑是什么 - Implicit super constructor Num() is undefined for default constructor. Must define an explicit constructor, what is the logic behind this Gson序列化名称:“默认构造函数未定义隐式超级构造函数Object()。 必须定义一个明确的构造函数” - Gson serialized name: “Implicit super constructor Object() is undefined for default constructor. Must define an explicit constructor” “默认构造函数的隐式超级构造函数BaseTestMethod()未定义。 必须定义一个显式构造函数”如何解决此错误? - “Implicit super constructor BaseTestMethod() is undefined for default constructor. Must define an explicit constructor” How do i fix this error? 默认构造函数无法处理隐式超级构造函数抛出的异常类型异常 - Default constructor cannot handle exception type Exception thrown by implicit super constructor 默认构造函数无法处理隐式超级构造函数引发的异常类型ioexception - default constructor cannot handle exception type ioexception thrown by implicit super constructor 默认构造函数无法处理由隐式超级构造函数抛出的异常类型SocketException - Default constructor cannot handle exception type SocketException thrown by implicit super constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM