You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Ed Young <ej...@SummitBid.com> on 2007/04/27 18:51:20 UTC

How to access a file that's inside an executable jar assembly, programatically

I'd like to display the license information contained in a file that
has been assembled into an executable jar file assembly when the user
passes a command line parameter.

I'm not sure if this is a maven issue, really, but the executable
assembly was created using the maven assembly plugin.

When viewing the jar file contents, the file appears in the top level
of the jar:
...
  -rw-r--r--       137  27-Apr-2007  10:24:08
META-INF/maven/com.mycompany/crawler-common/pom.properties
  -rw-r--r--       967  23-Apr-2007  14:05:36
META-INF/maven/com.mycompany/crawler-common/pom.xml
  -rw-r--r--     14918  27-Apr-2007  10:24:08
com/mycompany/crawler/console/ConsoleApp.class
  -rw-r--r--      2827  27-Apr-2007  10:24:08  LICENSE.apache_1.1.txt
  -rw-r--r--     10317  27-Apr-2007  10:24:08  LICENSE.apache_2.0.txt
  -rw-r--r--     11950  27-Apr-2007  10:24:08  LICENSE.cpl_1.0.txt
  -rw-r--r--      4083  27-Apr-2007  10:24:08  LICENSE.dom-documentation.txt
  -rw-r--r--      3595  27-Apr-2007  10:24:08  LICENSE.dom-software.txt
  -rw-r--r--       804  27-Apr-2007  10:24:08  LICENSE.sax.txt
  -rw-r--r--       731  27-Apr-2007  10:24:08  log4j.properties
  -rw-r--r--       138  27-Apr-2007  10:24:08
META-INF/maven/com.mycompany/mycompany-console/pom.properties

If I want to display the LICENSE.apache_1.1.txt file, how can I access
it programatically?

I tried the simple
        File testFilePresence = new File("LICENSE.apache_1.1.txt");
        if(testFilePresence.exists())
        {
            logger.info("file " + testFilePresence.getName()+" is visible");
        }
        else
        {
            logger.info("file " + testFilePresence.getAbsolutePath()+"
is NOT visible");
            logger.info("file " + testFilePresence.getName()+" is NOT
visible");
        }

and it is not visible.

Thanks in advance.

-- 
Ed

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to access a file that's inside an executable jar assembly, programatically

Posted by Ed Young <ej...@SummitBid.com>.
Thanks guys! That did the trick.

On 4/27/07, Wayne Fay <wa...@gmail.com> wrote:
> John Casey sent this method a while ago that might be useful:
>
> private File getFile( String resourceName )
> {
>    ClassLoader cloader = Thread.currentThread().getContextClassLoader();
>    URL resource = cloader.getResource( resourceName );
>
>    if ( resource == null )
>    {
>        throw new IllegalArgumentException( "Could not get resource: " +
> resourceName );
>    }
>
>    return new File( resource.getPath() );
> }
>
> Wayne
>
> On 4/27/07, Heinrich Nirschl <he...@gmail.com> wrote:
> > On 4/27/07, Ed Young <ej...@summitbid.com> wrote:
> > > I'd like to display the license information contained in a file that
> > > has been assembled into an executable jar file assembly when the user
> > > passes a command line parameter.
> > >
> > > I'm not sure if this is a maven issue, really, but the executable
> > > assembly was created using the maven assembly plugin.
> >
> > Indeed not a maven issue. You have to load it as a resource. This link
> > should help:
> > http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
> >
> > - Henry
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>


-- 
Ed

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to access a file that's inside an executable jar assembly, programatically

Posted by Wayne Fay <wa...@gmail.com>.
John Casey sent this method a while ago that might be useful:

private File getFile( String resourceName )
{
   ClassLoader cloader = Thread.currentThread().getContextClassLoader();
   URL resource = cloader.getResource( resourceName );

   if ( resource == null )
   {
       throw new IllegalArgumentException( "Could not get resource: " +
resourceName );
   }

   return new File( resource.getPath() );
}

Wayne

On 4/27/07, Heinrich Nirschl <he...@gmail.com> wrote:
> On 4/27/07, Ed Young <ej...@summitbid.com> wrote:
> > I'd like to display the license information contained in a file that
> > has been assembled into an executable jar file assembly when the user
> > passes a command line parameter.
> >
> > I'm not sure if this is a maven issue, really, but the executable
> > assembly was created using the maven assembly plugin.
>
> Indeed not a maven issue. You have to load it as a resource. This link
> should help:
> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
>
> - Henry
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: How to access a file that's inside an executable jar assembly, programatically

Posted by Heinrich Nirschl <he...@gmail.com>.
On 4/27/07, Ed Young <ej...@summitbid.com> wrote:
> I'd like to display the license information contained in a file that
> has been assembled into an executable jar file assembly when the user
> passes a command line parameter.
>
> I'm not sure if this is a maven issue, really, but the executable
> assembly was created using the maven assembly plugin.

Indeed not a maven issue. You have to load it as a resource. This link
should help:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

- Henry

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org