You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Simon Lehmann <si...@gmx.de> on 2008/11/25 03:28:51 UTC

Running a standalone application built with maven

Hello everyone,

I am currently trying to use maven for building my project. I have read
and followed the documentation found in the Maven Users Centre and it
really works quite well so far.

The problem I have now is, that I don't know how to run my application
once it is built with, say 'mvn package'. I tried 'java -jar
target/myproject.jar' (in the base directory of the project), but then
it doesn't find the jars it depends on. I have jaudiotagger and
sqlite-jdbc as a dependency, but it only cannot find sqlite-jdbc, which
I only need at runtime (and have specified as such).

After some searching I have found a way to solve the problem of missing
dependencies, by using 'java -cp $CLASSPATH:target/myproject.jar
my.package.MainClass', but then it doesn't find any resources I try to
load in my code with
"this.getClass().getClassLoader().getResource(...)". All resources are
placed in the src/main/resources directory. If I have no dependecies and
thus can run the application with "java -jar target/myproject.jar" it
finds the resources.

Is there some way to simplify this, say, by automatically generating a
startup script which can be used to start the application without having
to worry about all these things?

Thanks a lot,

Simon Lehmann

RE: Running a standalone application built with maven

Posted by "Inman, Peter" <pe...@mcpplc.com>.
I use the assembly plugin and it works really well.

I've just started to learn and use maven with a stand alone application
and so far I'm hooked! 

We have to move the full j2ee app soon, but this is a good starting
point.

Pete

-----Original Message-----
From: Simon Lehmann [mailto:simon.lehmann@gmx.de] 
Sent: 25 November 2008 02:29
To: users@maven.apache.org
Subject: Running a standalone application built with maven

Hello everyone,

I am currently trying to use maven for building my project. I have read
and followed the documentation found in the Maven Users Centre and it
really works quite well so far.

The problem I have now is, that I don't know how to run my application
once it is built with, say 'mvn package'. I tried 'java -jar
target/myproject.jar' (in the base directory of the project), but then
it doesn't find the jars it depends on. I have jaudiotagger and
sqlite-jdbc as a dependency, but it only cannot find sqlite-jdbc, which
I only need at runtime (and have specified as such).

After some searching I have found a way to solve the problem of missing
dependencies, by using 'java -cp $CLASSPATH:target/myproject.jar
my.package.MainClass', but then it doesn't find any resources I try to
load in my code with
"this.getClass().getClassLoader().getResource(...)". All resources are
placed in the src/main/resources directory. If I have no dependecies and
thus can run the application with "java -jar target/myproject.jar" it
finds the resources.

Is there some way to simplify this, say, by automatically generating a
startup script which can be used to start the application without having
to worry about all these things?

Thanks a lot,

Simon Lehmann

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


Re: Running a standalone application built with maven

Posted by Stephen Connolly <st...@gmail.com>.
2008/11/25 Wayne Fay <wa...@gmail.com>

>
> > load in my code with
> > "this.getClass().getClassLoader().getResource(...)". All resources are
>
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

There are issues with that method and windows file paths that have spaces in
them.

AFAIR, it's better to do

return new File( resource.toURI() );

Re: Running a standalone application built with maven

Posted by Simon Lehmann <si...@gmx.de>.
> 
> Unless you have a priori knowledge that in every possible environment
> you'll actually have a File, the best thing is to open a stream from 
> that URL directly.
> </PetPeeve>
> 

I actually do this already (getResourceAsStream()), but good to point
that out (in case someone else might read that). This is one of the
things I have learned when it comes to resource loading: never assume
anything.

> I don't see why you should need to use the context classloader if the 
> resources are within the same domain (realm? scope? I'm looking for a 
> word which is a more formalized form of 'in the same JAR') as the class 
> you were referencing with 
> this.getClass().getClassLoader().getResource(). I'm assuming the 
> resources in question are definitely being included in the produced JARs?

Yes, the resources will be included in the JAR. I don't know if it's
actually necessary to go via the context classloader, but it seems to
work well so far. My original approach has usually worked too, but not
in this case. But this might be also caused by the way how I am
currently starting the application.

As I already said: I don't know why this has to be so complicated. The
whole JAR/resource concept is quite nice, but its implementation...
everytime I think I have understood how everything works, I find
something which does not work as expected. If someone can point me
towards some good books, articles, documentation on this subject, I
would be really thankful.

Simon

Re: Running a standalone application built with maven

Posted by John Stoneham <ly...@lyrically.net>.
>> 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() );
>> }

<PetPeeve>
Please be careful pulling resources like this. Those of us who've coded 
with OSGi know that the URLs from ClassLoader.getResource() are not 
necessarily resolvable to a path that will successfully yield a File. (I 
owe the WebLogic team a long and painful chat about this one day.)

Unless you have a priori knowledge that in every possible environment
you'll actually have a File, the best thing is to open a stream from 
that URL directly.
</PetPeeve>

I don't see why you should need to use the context classloader if the 
resources are within the same domain (realm? scope? I'm looking for a 
word which is a more formalized form of 'in the same JAR') as the class 
you were referencing with 
this.getClass().getClassLoader().getResource(). I'm assuming the 
resources in question are definitely being included in the produced JARs?

- John

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


Re: Running a standalone application built with maven

Posted by Simon Lehmann <si...@gmx.de>.
Am Montag, den 24.11.2008, 22:38 -0800 schrieb Wayne Fay:
> > The problem I have now is, that I don't know how to run my application
> > once it is built with, say 'mvn package'. I tried 'java -jar
> 
> People generally use the assembly plugin for this purpose.
> http://maven.apache.org/plugins/maven-assembly-plugin/
> 
> > my.package.MainClass', but then it doesn't find any resources I try to
> > load in my code with
> > "this.getClass().getClassLoader().getResource(...)". All resources are
> 
> 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
> 

Thank you (and Peter). The assembly plugin is exactly what I was looking
for.

The method for getting file resources seems to work well. I think I even
understand what is causing all the problems with the different
getResource() methods, but then I still wonder why it has to be this
complicated... but thats a completely different subject, which doesn't
even belong here I think.

Simon

Re: Running a standalone application built with maven

Posted by Wayne Fay <wa...@gmail.com>.
> The problem I have now is, that I don't know how to run my application
> once it is built with, say 'mvn package'. I tried 'java -jar

People generally use the assembly plugin for this purpose.
http://maven.apache.org/plugins/maven-assembly-plugin/

> my.package.MainClass', but then it doesn't find any resources I try to
> load in my code with
> "this.getClass().getClassLoader().getResource(...)". All resources are

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

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