Results 1 to 6 of 6

Thread: How to load a resource from within a jar?

  1. #1

    Default How to load a resource from within a jar?

    Hi,

    I have a resource file that is contained within a jar file. When I try to use classpath: to point to the resource Spring throws an error:

    Code:
    caused by: java.io.FileNotFoundException: class path resource [fwl/localization/ssh_key_tx3.private] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/tomcat/apache-tomcat-7.0.14/webapps/fwl/WEB-INF/lib/java-1.0-SNAPSHOT.jar!/fwl/localization/ssh_key_tx3.private
    But when I look in the java-1.0-SNAPSHOT.jar file, the file is present (fwl/localization/ssh_key_tx3.private).

    A little quick search found me this link with someone who is having a similar problem, but with no response.
    http://forum.springsource.org/showth...exploded-wars)

    Is there any mechanism in Spring that will allow me to load a resource file from within a jar file?

    Thanks,

    Eric

  2. #2

    Default

    Hi Eric,

    Importing a xml file from a jar file works for me.

    Did you do

    <import resource="classpath:/fwl/localization/ssh_key_tx3.private" />

    by any chance?

    try specifying classpath:

    by doing this, spring should find the file in classpath ...

    Please paste the part where u r importing that .private file.

    Thanks
    Javaid

  3. #3
    Join Date
    Apr 2010
    Posts
    20

    Default

    I have found that using the import only allows for 1 JAR file to have that path. For Example:

    SubModule1.jar
    /META-INF/spring/test/module1-context.xml

    SubModule2.jar
    /META-INF/spring/test/module2-context.xml

    MainModule.jar
    /applicationContext
    <import resource="classpath:/META-INF/spring/test/**/*.xml"/>

    It looks like Spring finds the first JAR file in the Classpath that contains the path '/META-INF/spring/test' and used it. Spring will then ignore any other JAR file in the Classpath afterwards.

    Is there a way to ensure Spring will search all JAR files and directories for the specified path?

  4. #4
    Join Date
    Apr 2010
    Posts
    20

    Default

    Ahhh...figured it out....

    <import resource="classpath*:/META-INF/test/**/*.xml"/>

  5. #5

    Default

    Quote Originally Posted by Javaid Bolaky View Post
    Hi Eric,

    Importing a xml file from a jar file works for me.

    Did you do

    <import resource="classpath:/fwl/localization/ssh_key_tx3.private" />

    by any chance?

    try specifying classpath:
    Sorry for the delay in my response. I'm not importing it as a resource as such as the object is expecting a resource name as a parameter, and it in turn is loading the resource:
    Code:
    	<bean id="sftpSessionFactory"
    		class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
    		<property name="host" value="localhost" />
     		<property name="privateKey"	value="classpath:fwl/localization/ssh_key_tx3.private" /> 
    		<property name="port" value="22" />
    		<property name="user" value="fwl" />
    	</bean>
    DefaultSftpSessionFactory then does the following:
    Code:
    String prvfile = this.privateKey.getFile().getAbsolutePath()
    which it then tries to read as:
    Code:
          file=new File(prvfile);
          fis=new FileInputStream(prvfile);
    And then throws an error that the file cannot be found.

    Any thoughts?

    Tx,

    Eric

  6. #6
    Join Date
    Nov 2011
    Posts
    1

    Default

    I had this issue also. The DefaultSftpSessionFactory is adding an Identity using a method that requires a path to the private key:

    Code:
    addIdentity(String prvkey, String passphrase)
    There is another method that could be used, one which allows the Identity to be passed in as a byte array:

    Code:
    public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase)
    In the end I created my own FtpSessionFactory that used this method loading a byte array from an InputStream (which can be created from a resource inside a jar). However if I wanted to use the Spring SftpSession object, I would have to put my FtpSessionFactory inside a org.springframework.integration.sftp.session package as the class is package scoped. I don't really want to do that so I'm looking at some other options now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •