Reputation: 1040
I am trying to access the file inside a jar but getting exception
String tarFilePath = "data/";
My code looks like below I am trying in couple of ways one by reading as stream and trying to access from stream
InputStream in1 = getClass().getResourceAsStream(tarFilePath + "test_data.tar.gz");
TarArchiveInputStream tarArchiveInputStream= new TarArchiveInputStream(new GzipCompressorInputStream(in1));
java.lang.NullPointerException
at org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream.<init>(GzipCompressorInputStream.java:128)
at org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream.<init>(GzipCompressorInputStream.java:100)
at com.motive.systemtest.stepsdef.HttpReceiverSteps.given_method(HttpReceiverSteps.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2nd way
File file = new File(getClass().getClassLoader().getResource(tarFilePath + tarFileName).getFile());
java.io.FileNotFoundException: file:/home/username/Projects/systemtest/target/systemtest-1.0.0-fat-tests.jar!/data/test_data.tar.gz (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at org.apache.http.entity.FileEntity.writeTo(FileEntity.java:94)
at org.apache.http.entity.HttpEntityWrapper.writeTo(HttpEntityWrapper.java:94)
at org.apache.http.impl.client.EntityEnclosingRequestWrapper$EntityWrapper.writeTo(EntityEnclosingRequestWrapper.java:112)
at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:118)
at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:266)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.sendRequestEntity(ManagedClientConnectionImpl.java:216)
at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:238)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:686)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:488)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at com.motive.systemtest.stepsdef.HttpReceiverSteps.postChunkFileHttpReceiver(HttpReceiverSteps.java:146)
at com.motive.systemtest.stepsdef.HttpReceiverSteps.given_method(HttpReceiverSteps.java:66)
1 st way throws null pointer exception and second throws file not found exception .
Is it possible to access resource under a jar as file Please check and help me on this
Upvotes: 0
Views: 1274
Reputation: 838
Your first attempt should work, only tarFilePath must start with a slash.
getClass().getResourceAsStream("/data/test_data.tar.gz")
begins from the root directory of your jar.
getClass().getResourceAsStream("data/test_data.tar.gz")
looks into a subdirectory relative to your class file.
Upvotes: 1