title: Get Resource URL Path in Java Spring
body: <p>Trying to get the path of a JSON file I have in my resource folder but failing to do so with <code>getResource().getPath()</code>.  I would like to get this URL in as robust a manner as possible.</p>
  <pre><code>public static void initializeFirebase() {
          try {
              URL url = getResource(&quot;google-services.json&quot;);
              System.out.println(url.getPath());
              FirebaseOptions options = new FirebaseOptions
                      .Builder()
                      .setServiceAccount(new FileInputStream(url.getPath()))
                      .setDatabaseUrl(&quot;https://XXXXX.firebaseio.com/&quot;)
                      .build();
              FirebaseApp.initializeApp(options);
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  </code></pre>
  <p>The above function prints:
  <code>/Users/username/Documents/Spring_Project/springbackend/build/resources/main/google-services.json</code> before throwing the error:</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.RuntimeException: Failed to read service account
      at com.google.firebase.FirebaseOptions$Builder.setServiceAccount(FirebaseOptions.java:82)
      at live.cibc.labs.springbackend.SpringbackendApplication.initializeFirebase(SpringbackendApplication.java:188)
      at live.cibc.labs.springbackend.SpringbackendApplication.main(SpringbackendApplication.java:61)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  Caused by: org.json.JSONException: JSONObject[&quot;project_id&quot;] not found.
  </code></pre>
  <p>Obviously it cannot find my project_id, as my google-services.json is under <code>src/main/resources/google-services.json</code>.</p>
  <p>How should I fetch the path is as robust a manner as possible?</p>
  <h3>google-services.json</h3>
  <pre><code>{
    &quot;project_info&quot;: {
      &quot;project_number&quot;: &quot;XXXXXXXX&quot;,
      &quot;firebase_url&quot;: &quot;https://XXXXXX.firebaseio.com&quot;,
      &quot;project_id&quot;: &quot;XXXX-XXX&quot;,
      &quot;storage_bucket&quot;: &quot;XXXXXXXX.appspot.com&quot;
    },
    &quot;client&quot;: [
      {
        &quot;client_info&quot;: {
          &quot;mobilesdk_app_id&quot;: &quot;1:XXXXXXXXX:android:XXXXXXX&quot;,
          &quot;android_client_info&quot;: {
            &quot;package_name&quot;: &quot;my.spring.project&quot;
          }
        },
        &quot;oauth_client&quot;: [
          {
            &quot;client_id&quot;: &quot;XXXXX-XXXXX.apps.googleusercontent.com&quot;,
            &quot;client_type&quot;: 3
          }
        ],
        &quot;api_key&quot;: [
          {
            &quot;current_key&quot;: &quot;XXXXXX&quot;
          }
        ],
        &quot;services&quot;: {
          &quot;analytics_service&quot;: {
            &quot;status&quot;: 1
          },
          &quot;appinvite_service&quot;: {
            &quot;status&quot;: 1,
            &quot;other_platform_oauth_client&quot;: []
          },
          &quot;ads_service&quot;: {
            &quot;status&quot;: 2
          }
        }
      }
    ],
    &quot;configuration_version&quot;: &quot;1&quot;
  }
  </code></pre>