Announcement Announcement Module
Collapse
No announcement yet.
How to load a resource from within a jar? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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?

      Comment


      • #4
        Ahhh...figured it out....

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

        Comment


        • #5
          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

          Comment


          • #6
            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.

            Comment


            • #7
              Originally posted by turquhart View Post
              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.
              I'm currently facing the same issue, and tried to create my own FtpSessionFactory, but I'm at a loss of how to get over the speedbump of figuring out how to get around using the SftpSession object. Any hints as to where to start?

              Comment

              Working...
              X