FileNotFoundException while running jar file [duplicate]












-3
















This question already has an answer here:




  • getResource() to a file at runtime

    2 answers




While trying to run this code,



public Session buildSSHConnection() throws Exception{

String user = "user";
int port = 22;
Session session = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(this.getClass().getResource("beispiel.key").getFile());
session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
}catch(JSchException e) {
session = null;
throw e;
}
return session;
}


in my runnable jar file, I always get a FileNotFoundException.



I know the file beispiel.key exists in my jar file, as I checked via



jar -tf ssh.jar 


The Stacktrace is



com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:543)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:407)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:367)
at SSHConnection.buildSSHConnection(SSHConnection.java:49)
at SSHConnection.main(SSHConnection.java:64)

Caused by: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.jcraft.jsch.Util.fromFile(Util.java:508)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:540)
... 5 more


I found a working alternative for my code:



JSch jsch = new JSch();
InputStream in = this.getClass().getResourceAsStream("beispiel.key");
byte c = new byte[in.available()];
in.read(c);
jsch.addIdentity("rsa", c, null, null);


Accessing the file as a Stream works. I didn't try that first because I had a problem finding out the size for my byte array.










share|improve this question















marked as duplicate by Robin Green, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 6 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Please provide a Minimal, Complete, and Verifiable example and the full stack trace of your exception. It's hard to guess what your application is doing and what's wrong.

    – Selaron
    Nov 26 '18 at 10:54











  • what is the path you have given for the file?

    – Pirate
    Nov 26 '18 at 13:53











  • The 'file' you are trying to open is within your application jar file. You can't access it as a file (which expects it on the file system, not inside a jar file).

    – Mark Rotteveel
    Dec 6 '18 at 15:27
















-3
















This question already has an answer here:




  • getResource() to a file at runtime

    2 answers




While trying to run this code,



public Session buildSSHConnection() throws Exception{

String user = "user";
int port = 22;
Session session = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(this.getClass().getResource("beispiel.key").getFile());
session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
}catch(JSchException e) {
session = null;
throw e;
}
return session;
}


in my runnable jar file, I always get a FileNotFoundException.



I know the file beispiel.key exists in my jar file, as I checked via



jar -tf ssh.jar 


The Stacktrace is



com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:543)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:407)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:367)
at SSHConnection.buildSSHConnection(SSHConnection.java:49)
at SSHConnection.main(SSHConnection.java:64)

Caused by: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.jcraft.jsch.Util.fromFile(Util.java:508)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:540)
... 5 more


I found a working alternative for my code:



JSch jsch = new JSch();
InputStream in = this.getClass().getResourceAsStream("beispiel.key");
byte c = new byte[in.available()];
in.read(c);
jsch.addIdentity("rsa", c, null, null);


Accessing the file as a Stream works. I didn't try that first because I had a problem finding out the size for my byte array.










share|improve this question















marked as duplicate by Robin Green, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 6 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Please provide a Minimal, Complete, and Verifiable example and the full stack trace of your exception. It's hard to guess what your application is doing and what's wrong.

    – Selaron
    Nov 26 '18 at 10:54











  • what is the path you have given for the file?

    – Pirate
    Nov 26 '18 at 13:53











  • The 'file' you are trying to open is within your application jar file. You can't access it as a file (which expects it on the file system, not inside a jar file).

    – Mark Rotteveel
    Dec 6 '18 at 15:27














-3












-3








-3









This question already has an answer here:




  • getResource() to a file at runtime

    2 answers




While trying to run this code,



public Session buildSSHConnection() throws Exception{

String user = "user";
int port = 22;
Session session = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(this.getClass().getResource("beispiel.key").getFile());
session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
}catch(JSchException e) {
session = null;
throw e;
}
return session;
}


in my runnable jar file, I always get a FileNotFoundException.



I know the file beispiel.key exists in my jar file, as I checked via



jar -tf ssh.jar 


The Stacktrace is



com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:543)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:407)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:367)
at SSHConnection.buildSSHConnection(SSHConnection.java:49)
at SSHConnection.main(SSHConnection.java:64)

Caused by: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.jcraft.jsch.Util.fromFile(Util.java:508)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:540)
... 5 more


I found a working alternative for my code:



JSch jsch = new JSch();
InputStream in = this.getClass().getResourceAsStream("beispiel.key");
byte c = new byte[in.available()];
in.read(c);
jsch.addIdentity("rsa", c, null, null);


Accessing the file as a Stream works. I didn't try that first because I had a problem finding out the size for my byte array.










share|improve this question

















This question already has an answer here:




  • getResource() to a file at runtime

    2 answers




While trying to run this code,



public Session buildSSHConnection() throws Exception{

String user = "user";
int port = 22;
Session session = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(this.getClass().getResource("beispiel.key").getFile());
session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
}catch(JSchException e) {
session = null;
throw e;
}
return session;
}


in my runnable jar file, I always get a FileNotFoundException.



I know the file beispiel.key exists in my jar file, as I checked via



jar -tf ssh.jar 


The Stacktrace is



com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:543)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:407)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:367)
at SSHConnection.buildSSHConnection(SSHConnection.java:49)
at SSHConnection.main(SSHConnection.java:64)

Caused by: java.io.FileNotFoundException: file:/home/sebastian/Schreibtisch/ssh.jar!/beispiel.key (Datei oder Verzeichnis nicht gefunden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.jcraft.jsch.Util.fromFile(Util.java:508)
at com.jcraft.jsch.KeyPair.load(KeyPair.java:540)
... 5 more


I found a working alternative for my code:



JSch jsch = new JSch();
InputStream in = this.getClass().getResourceAsStream("beispiel.key");
byte c = new byte[in.available()];
in.read(c);
jsch.addIdentity("rsa", c, null, null);


Accessing the file as a Stream works. I didn't try that first because I had a problem finding out the size for my byte array.





This question already has an answer here:




  • getResource() to a file at runtime

    2 answers








java rsa executable-jar filenotfoundexception jsch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 13:52









Nicholas K

6,93061233




6,93061233










asked Nov 26 '18 at 10:47









aquila105aquila105

12




12




marked as duplicate by Robin Green, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 6 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Robin Green, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 6 '18 at 15:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Please provide a Minimal, Complete, and Verifiable example and the full stack trace of your exception. It's hard to guess what your application is doing and what's wrong.

    – Selaron
    Nov 26 '18 at 10:54











  • what is the path you have given for the file?

    – Pirate
    Nov 26 '18 at 13:53











  • The 'file' you are trying to open is within your application jar file. You can't access it as a file (which expects it on the file system, not inside a jar file).

    – Mark Rotteveel
    Dec 6 '18 at 15:27



















  • Please provide a Minimal, Complete, and Verifiable example and the full stack trace of your exception. It's hard to guess what your application is doing and what's wrong.

    – Selaron
    Nov 26 '18 at 10:54











  • what is the path you have given for the file?

    – Pirate
    Nov 26 '18 at 13:53











  • The 'file' you are trying to open is within your application jar file. You can't access it as a file (which expects it on the file system, not inside a jar file).

    – Mark Rotteveel
    Dec 6 '18 at 15:27

















Please provide a Minimal, Complete, and Verifiable example and the full stack trace of your exception. It's hard to guess what your application is doing and what's wrong.

– Selaron
Nov 26 '18 at 10:54





Please provide a Minimal, Complete, and Verifiable example and the full stack trace of your exception. It's hard to guess what your application is doing and what's wrong.

– Selaron
Nov 26 '18 at 10:54













what is the path you have given for the file?

– Pirate
Nov 26 '18 at 13:53





what is the path you have given for the file?

– Pirate
Nov 26 '18 at 13:53













The 'file' you are trying to open is within your application jar file. You can't access it as a file (which expects it on the file system, not inside a jar file).

– Mark Rotteveel
Dec 6 '18 at 15:27





The 'file' you are trying to open is within your application jar file. You can't access it as a file (which expects it on the file system, not inside a jar file).

– Mark Rotteveel
Dec 6 '18 at 15:27












1 Answer
1






active

oldest

votes


















0














Try to set the package name where the file is located in this line:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());


If don't works, try to set a slash before the package name, like:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());





share|improve this answer
























  • it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

    – aquila105
    Nov 26 '18 at 12:35











  • Try use getResourceAsStream.

    – snake
    Nov 26 '18 at 12:51


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try to set the package name where the file is located in this line:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());


If don't works, try to set a slash before the package name, like:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());





share|improve this answer
























  • it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

    – aquila105
    Nov 26 '18 at 12:35











  • Try use getResourceAsStream.

    – snake
    Nov 26 '18 at 12:51
















0














Try to set the package name where the file is located in this line:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());


If don't works, try to set a slash before the package name, like:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());





share|improve this answer
























  • it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

    – aquila105
    Nov 26 '18 at 12:35











  • Try use getResourceAsStream.

    – snake
    Nov 26 '18 at 12:51














0












0








0







Try to set the package name where the file is located in this line:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());


If don't works, try to set a slash before the package name, like:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());





share|improve this answer













Try to set the package name where the file is located in this line:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());


If don't works, try to set a slash before the package name, like:



jsch.addIdentity(this.getClass().getResource("THE_PACKAGE_NAMEbeispiel.key").getFile());






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 12:07









snakesnake

1




1













  • it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

    – aquila105
    Nov 26 '18 at 12:35











  • Try use getResourceAsStream.

    – snake
    Nov 26 '18 at 12:51



















  • it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

    – aquila105
    Nov 26 '18 at 12:35











  • Try use getResourceAsStream.

    – snake
    Nov 26 '18 at 12:51

















it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

– aquila105
Nov 26 '18 at 12:35





it didn't work. I still get the same Stacktrace. The only thing different is that the path includes the package name now.

– aquila105
Nov 26 '18 at 12:35













Try use getResourceAsStream.

– snake
Nov 26 '18 at 12:51





Try use getResourceAsStream.

– snake
Nov 26 '18 at 12:51





Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)