classpath when running grails run-app

View: New views
8 Messages — Rating Filter:   Alert me  

classpath when running grails run-app

by Dave Doty-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

When I run a Java program or a junit test in my grails project, the classpath is extensive. The following is a partial printing of the classpath when I run a JUnit test (from within Intellij):

***
E:\grails\lib\standard-2.3.jar
E:\grails\dist\grails-cli-1.0.3.jar
E:\grails\lib\spring-binding-2.0-m1.jar
... dozens more
***

However, I need access to some libraries in my project's lib subdirectory while my program is running as well. In particular, I run some junit tests from within the program, so I want access to junit.jar (placed in the lib directory). When I run grails run-app (either from the command line or from within Intellij) and print the classpath, it is rather stark:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

I cannot determine how to include more jar files on the classpath when running grails run-app. I was under the impression that anything in the lib directory would be included, but that is obviously true only for compilation.

Dave

Re: classpath when running grails run-app

by Jan Ehrhardt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The archives in the lib folder should be on the classpath too, while using grails run-app.

Jan

On Thu, Jul 3, 2008 at 7:28 AM, Dave Doty <ddoty@...> wrote:
When I run a Java program or a junit test in my grails project, the classpath is extensive. The following is a partial printing of the classpath when I run a JUnit test (from within Intellij):

***
E:\grails\lib\standard-2.3.jar
E:\grails\dist\grails-cli-1.0.3.jar
E:\grails\lib\spring-binding-2.0-m1.jar
... dozens more
***

However, I need access to some libraries in my project's lib subdirectory while my program is running as well. In particular, I run some junit tests from within the program, so I want access to junit.jar (placed in the lib directory). When I run grails run-app (either from the command line or from within Intellij) and print the classpath, it is rather stark:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

I cannot determine how to include more jar files on the classpath when running grails run-app. I was under the impression that anything in the lib directory would be included, but that is obviously true only for compilation.

Dave


Re: classpath when running grails run-app

by Dave Doty-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jul 3, 2008 at 12:46 AM, Jan Ehrhardt <jan.ehrhardt@...> wrote:
The archives in the lib folder should be on the classpath too, while using grails run-app.
 
But they aren't, in my case, unless calling System.getProperty("java.class.path") from within my BootStrap.groovy file is not the correct way to determine what grails considers the runtime classpath to be.

Can someone point me to a config file/start-up script/etc to look at? In other words, what order of events is supposed to ensure that jar files in <project-home>/lib are placed on the classpath, so that I can trace through to try to determine what is going wrong?

Dave

Re: classpath when running grails run-app

by Wolfgang Schell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi!


Dave Doty-2 wrote:
> The archives in the lib folder should be on the classpath too, while using
> grails run-app.
>

But they aren't, in my case, unless calling
System.getProperty("java.class.path") from within my BootStrap.groovy file
is not the correct way to determine what grails considers the runtime
classpath to be.
As far as I know, the classes are managed by either a GroovyClassLoader (http://groovy.codehaus.org/api/groovy/lang/GroovyClassLoader.html) or GrailsClassLoader (http://bamboo.ci.codehaus.org/download/GRAILS-GRAILS15/artifacts/build-1348/JavaDocs/api/org/codehaus/groovy/grails/compiler/GrailsClassLoader.html).

You could try to get the class path using an expression something like this:

MyDomain.class.classLoader.classpath

(see javadoc of GroovyClassLoader linked to above)

HTH,

Wolfgang

Re: classpath when running grails run-app

by Dave Doty-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Jul 3, 2008 at 12:46 AM, Jan Ehrhardt <jan.ehrhardt@...> wrote:
The archives in the lib folder should be on the classpath too, while using grails run-app.

Jan

This does not appear to be true on my system. Using version 1.0.3 of grails, take the following steps:

1) Run "grails create-app Test"
2) Place a jar file in Test/lib (I used junit-4.4.jar from the JUnit 4.4 download).
3) Edit the init closure in Test/grails-app/conf/BootStrap.groovy to contain the following lines:

         println "\nIn BootStrap.groovy:\n***  "
         for (String property: System.getProperty("java.class.path").split(";")) {
             println property
         }
         println "***\n"

4) cd into Test and run "grails run-app"

You should see the following (or something like it depending on your directories) appear in the output:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

The same effect happens if I execute these statements in main.gsp, and then browse to http://localhost:8080/Test, so this is not a matter of timing (i.e., if the classpath changes happening after BootStrap.groovy executes).

It would seem, from this example, that jar files in the lib subdirectory are not placed on the classpath automatically, as Jan indicated.

Could someone please tell me how to manually alter the classpath when executing grails run-app? For instance, to alter the classpath while running a Java program, one would type "java -cp lib/junit-4.4.jar MyProgram" at the command line. Is there a similar flag that can be used when executing grails from the command line? Is there a configuration file or startup script somewhere that can be altered?

Wolfgang suggested that I look at GroovyClassLoader and GrailsClassLoader, but since these are not classes whose source code I have control over, I don't know how to change them, and I don't know how to get access to the instance of either of them used by Grails, so that I can call methods (such as setClasspath) on them. The code suggested by Wolfgang obtains a reference to an object of type URLClassLoader, not GroovyClassLoader or GrailsClassLoader, and URLClassLoader does not appear to have methods that allows client code to alter the classpath.

Thank you,
Dave




On Thu, Jul 3, 2008 at 7:28 AM, Dave Doty <ddoty@...> wrote:
When I run a Java program or a junit test in my grails project, the classpath is extensive. The following is a partial printing of the classpath when I run a JUnit test (from within Intellij):

***
E:\grails\lib\standard-2.3.jar
E:\grails\dist\grails-cli-1.0.3.jar
E:\grails\lib\spring-binding-2.0-m1.jar
... dozens more
***

However, I need access to some libraries in my project's lib subdirectory while my program is running as well. In particular, I run some junit tests from within the program, so I want access to junit.jar (placed in the lib directory). When I run grails run-app (either from the command line or from within Intellij) and print the classpath, it is rather stark:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

I cannot determine how to include more jar files on the classpath when running grails run-app. I was under the impression that anything in the lib directory would be included, but that is obviously true only for compilation.

Dave




--
Dave

Re: classpath when running grails run-app

by Dave Doty-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I will assume this is a bug and will report it in jira.codehaus.org.

In the meantime, I would appreciate any advice anyone could offer about how to add a jar file to the classpath when running grails-app. I'm sure that it is very simple, but I just cannot figure out how to do it.

I suspect that Grails does something that *simulates* adding jar files in lib/ to the classpath. For instance, I can use JUnit classes within my Grails project code without error, so they are *somehow* being made aware of the existence of junit-4.4.jar. However, this is done without actually changing the classpath, as you can see from the example I gave below, and some applications (such as using the JSR-199 compiler API, which my project uses) require the actual classpath to be altered to contain more than just groovy-all-1.5.6.jar and grails-cli-1.0.3.jar. Attempting to use the JSR-199 compiler to compile a JUnit source file fails under grails, but does not fail when the JSR-199 compilation is initiated from within a normal java program that has the JUnit jar on the classpath.

Thank you,
Dave

On Thu, Jul 3, 2008 at 8:01 PM, Dave Doty <ddoty@...> wrote:
On Thu, Jul 3, 2008 at 12:46 AM, Jan Ehrhardt <jan.ehrhardt@...> wrote:
The archives in the lib folder should be on the classpath too, while using grails run-app.

Jan

This does not appear to be true on my system. Using version 1.0.3 of grails, take the following steps:

1) Run "grails create-app Test"
2) Place a jar file in Test/lib (I used junit-4.4.jar from the JUnit 4.4 download).
3) Edit the init closure in Test/grails-app/conf/BootStrap.groovy to contain the following lines:

         println "\nIn BootStrap.groovy:\n***  "
         for (String property: System.getProperty("java.class.path").split(";")) {
             println property
         }
         println "***\n"

4) cd into Test and run "grails run-app"

You should see the following (or something like it depending on your directories) appear in the output:


*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

The same effect happens if I execute these statements in main.gsp, and then browse to http://localhost:8080/Test, so this is not a matter of timing (i.e., if the classpath changes happening after BootStrap.groovy executes).

It would seem, from this example, that jar files in the lib subdirectory are not placed on the classpath automatically, as Jan indicated.

Could someone please tell me how to manually alter the classpath when executing grails run-app? For instance, to alter the classpath while running a Java program, one would type "java -cp lib/junit-4.4.jar MyProgram" at the command line. Is there a similar flag that can be used when executing grails from the command line? Is there a configuration file or startup script somewhere that can be altered?

Wolfgang suggested that I look at GroovyClassLoader and GrailsClassLoader, but since these are not classes whose source code I have control over, I don't know how to change them, and I don't know how to get access to the instance of either of them used by Grails, so that I can call methods (such as setClasspath) on them. The code suggested by Wolfgang obtains a reference to an object of type URLClassLoader, not GroovyClassLoader or GrailsClassLoader, and URLClassLoader does not appear to have methods that allows client code to alter the classpath.

Thank you,
Dave





On Thu, Jul 3, 2008 at 7:28 AM, Dave Doty <ddoty@...> wrote:
When I run a Java program or a junit test in my grails project, the classpath is extensive. The following is a partial printing of the classpath when I run a JUnit test (from within Intellij):

***
E:\grails\lib\standard-2.3.jar
E:\grails\dist\grails-cli-1.0.3.jar
E:\grails\lib\spring-binding-2.0-m1.jar
... dozens more
***

However, I need access to some libraries in my project's lib subdirectory while my program is running as well. In particular, I run some junit tests from within the program, so I want access to junit.jar (placed in the lib directory). When I run grails run-app (either from the command line or from within Intellij) and print the classpath, it is rather stark:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

I cannot determine how to include more jar files on the classpath when running grails run-app. I was under the impression that anything in the lib directory would be included, but that is obviously true only for compilation.

Dave




--
Dave



--
Dave

RE: classpath when running grails run-app

by Jay Guidos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi there,

 

I may be jumping in on this thread a bit late, and I admit I have not been following it all, but have you tried using the Grails rootLoader?  I have used that on occasion to add a jar at run time and it seems to work ok.

 

Jay

 


From: pexatus@... [mailto:pexatus@...] On Behalf Of Dave Doty
Sent: Monday, July 07, 2008 4:18 PM
To: user@...
Subject: Re: [grails-user] classpath when running grails run-app

 

I will assume this is a bug and will report it in jira.codehaus.org.

In the meantime, I would appreciate any advice anyone could offer about how to add a jar file to the classpath when running grails-app. I'm sure that it is very simple, but I just cannot figure out how to do it.

I suspect that Grails does something that *simulates* adding jar files in lib/ to the classpath. For instance, I can use JUnit classes within my Grails project code without error, so they are *somehow* being made aware of the existence of junit-4.4.jar. However, this is done without actually changing the classpath, as you can see from the example I gave below, and some applications (such as using the JSR-199 compiler API, which my project uses) require the actual classpath to be altered to contain more than just groovy-all-1.5.6.jar and grails-cli-1.0.3.jar. Attempting to use the JSR-199 compiler to compile a JUnit source file fails under grails, but does not fail when the JSR-199 compilation is initiated from within a normal java program that has the JUnit jar on the classpath.

Thank you,
Dave

On Thu, Jul 3, 2008 at 8:01 PM, Dave Doty <ddoty@...> wrote:

On Thu, Jul 3, 2008 at 12:46 AM, Jan Ehrhardt <jan.ehrhardt@...> wrote:

The archives in the lib folder should be on the classpath too, while using grails run-app.

Jan

 

This does not appear to be true on my system. Using version 1.0.3 of grails, take the following steps:

1) Run "grails create-app Test"
2) Place a jar file in Test/lib (I used junit-4.4.jar from the JUnit 4.4 download).
3) Edit the init closure in Test/grails-app/conf/BootStrap.groovy to contain the following lines:

         println "\nIn BootStrap.groovy:\n***  "
         for (String property: System.getProperty("java.class.path").split(";")) {
             println property
         }
         println "***\n"

4) cd into Test and run "grails run-app"

You should see the following (or something like it depending on your directories) appear in the output:



*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

The same effect happens if I execute these statements in main.gsp, and then browse to http://localhost:8080/Test, so this is not a matter of timing (i.e., if the classpath changes happening after BootStrap.groovy executes).

It would seem, from this example, that jar files in the lib subdirectory are not placed on the classpath automatically, as Jan indicated.

Could someone please tell me how to manually alter the classpath when executing grails run-app? For instance, to alter the classpath while running a Java program, one would type "java -cp lib/junit-4.4.jar MyProgram" at the command line. Is there a similar flag that can be used when executing grails from the command line? Is there a configuration file or startup script somewhere that can be altered?

Wolfgang suggested that I look at GroovyClassLoader and GrailsClassLoader, but since these are not classes whose source code I have control over, I don't know how to change them, and I don't know how to get access to the instance of either of them used by Grails, so that I can call methods (such as setClasspath) on them. The code suggested by Wolfgang obtains a reference to an object of type URLClassLoader, not GroovyClassLoader or GrailsClassLoader, and URLClassLoader does not appear to have methods that allows client code to alter the classpath.

Thank you,
Dave



 

On Thu, Jul 3, 2008 at 7:28 AM, Dave Doty <ddoty@...> wrote:

When I run a Java program or a junit test in my grails project, the classpath is extensive. The following is a partial printing of the classpath when I run a JUnit test (from within Intellij):

***
E:\grails\lib\standard-2.3.jar
E:\grails\dist\grails-cli-1.0.3.jar
E:\grails\lib\spring-binding-2.0-m1.jar
... dozens more
***

However, I need access to some libraries in my project's lib subdirectory while my program is running as well. In particular, I run some junit tests from within the program, so I want access to junit.jar (placed in the lib directory). When I run grails run-app (either from the command line or from within Intellij) and print the classpath, it is rather stark:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

I cannot determine how to include more jar files on the classpath when running grails run-app. I was under the impression that anything in the lib directory would be included, but that is obviously true only for compilation.

Dave

 



--
Dave




--
Dave


Re: classpath when running grails run-app

by Dave Doty-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jul 8, 2008 at 9:47 AM, Jay Guidos <jay.guidos@...> wrote:

Hi there,

I may be jumping in on this thread a bit late, and I admit I have not been following it all, but have you tried using the Grails rootLoader?  I have used that on occasion to add a jar at run time and it seems to work ok.

Jay

Graeme suggested as much on my bug report: (http://jira.codehaus.org/browse/GRAILS-3203?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel) that I access the jar files through this mechanism:

    getClass().classLoader.rootLoader.URLs.each { println it }

When executing that statement, the jar files in lib/ (along with lots of others) do indeed show up. But the problem is not that I cannot access the jar files in lib/. I can access them without having to go through the rootloader at all, since I know where they are stored. In fact, if I run the above code, I see the jar I need show up. The problem is that what the above code prints is not the actual classpath when executing grails run-app, and when I used the JSR-199 compiler, it seems to think that their is a real difference between jar files actually being on the classpath, versus being attached to the Grails rootloader.

For instance, if I run a simple Groovy script that uses the JSR-199 compiler to compile the following file (see this thread for the code that compiles it: http://www.nabble.com/JSR-199-Compiler-API-loses-runtime-annotations-when-run-under-grails-run-app-td18332256.html), it works as long as junit-4.4.jar is on the classpath:

    import org.junit.Before
    public class ExampleTest {
      @Before public void setUp() {}
    }

But the exact same compilation code, placed into (for example) BootStrap.groovy, and then run by executing grails run-app, fails because it cannot resolve the package org.junit. The JSR-199 compiler is not aware of junit-4.4.jar, presumably because it is not really on the classpath, even though it is attached to the rootloader. I can fix this problem by manually adding junit-4.4.jar using the -classpath option when running the JSR-199 compiler, but other problems get introduced; for instance, other classes in my project, which are referenced in the source code I am compiling, are not recognized. I suspect all these problems may be related to the difference between the classpaths that are seen by the JSR-199 compiler if it is called by grails run-app versus being called from a regular Groovy script.

Of course, it may be something different entirely, but the fact is that code that works in a normal Groovy script, using the JSR-199 compiler, fails when run under grails run-app. The only clue I have as to what variable could explain the difference is the difference in classpaths, so that is what I am trying to fix. But as Graeme stated in the bug report, it will not be fixed, so I am trying to find some workaround to be able to use the JSR-199 compiler from within a Grails application. My total ignorance of JSR-199, Grails, and Groovy is not helping matters.

Thanks,
Dave



From: pexatus@... [mailto:pexatus@...] On Behalf Of Dave Doty
Sent: Monday, July 07, 2008 4:18 PM
To: user@...
Subject: Re: [grails-user] classpath when running grails run-app

 

I will assume this is a bug and will report it in jira.codehaus.org.

In the meantime, I would appreciate any advice anyone could offer about how to add a jar file to the classpath when running grails-app. I'm sure that it is very simple, but I just cannot figure out how to do it.

I suspect that Grails does something that *simulates* adding jar files in lib/ to the classpath. For instance, I can use JUnit classes within my Grails project code without error, so they are *somehow* being made aware of the existence of junit-4.4.jar. However, this is done without actually changing the classpath, as you can see from the example I gave below, and some applications (such as using the JSR-199 compiler API, which my project uses) require the actual classpath to be altered to contain more than just groovy-all-1.5.6.jar and grails-cli-1.0.3.jar. Attempting to use the JSR-199 compiler to compile a JUnit source file fails under grails, but does not fail when the JSR-199 compilation is initiated from within a normal java program that has the JUnit jar on the classpath.

Thank you,
Dave

On Thu, Jul 3, 2008 at 8:01 PM, Dave Doty <ddoty@...> wrote:

On Thu, Jul 3, 2008 at 12:46 AM, Jan Ehrhardt <jan.ehrhardt@...> wrote:

The archives in the lib folder should be on the classpath too, while using grails run-app.

Jan

 

This does not appear to be true on my system. Using version 1.0.3 of grails, take the following steps:

1) Run "grails create-app Test"
2) Place a jar file in Test/lib (I used junit-4.4.jar from the JUnit 4.4 download).
3) Edit the init closure in Test/grails-app/conf/BootStrap.groovy to contain the following lines:

         println "\nIn BootStrap.groovy:\n***  "
         for (String property: System.getProperty("java.class.path").split(";")) {
             println property
         }
         println "***\n"

4) cd into Test and run "grails run-app"

You should see the following (or something like it depending on your directories) appear in the output:



*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

The same effect happens if I execute these statements in main.gsp, and then browse to http://localhost:8080/Test, so this is not a matter of timing (i.e., if the classpath changes happening after BootStrap.groovy executes).

It would seem, from this example, that jar files in the lib subdirectory are not placed on the classpath automatically, as Jan indicated.

Could someone please tell me how to manually alter the classpath when executing grails run-app? For instance, to alter the classpath while running a Java program, one would type "java -cp lib/junit-4.4.jar MyProgram" at the command line. Is there a similar flag that can be used when executing grails from the command line? Is there a configuration file or startup script somewhere that can be altered?

Wolfgang suggested that I look at GroovyClassLoader and GrailsClassLoader, but since these are not classes whose source code I have control over, I don't know how to change them, and I don't know how to get access to the instance of either of them used by Grails, so that I can call methods (such as setClasspath) on them. The code suggested by Wolfgang obtains a reference to an object of type URLClassLoader, not GroovyClassLoader or GrailsClassLoader, and URLClassLoader does not appear to have methods that allows client code to alter the classpath.

Thank you,
Dave



 

On Thu, Jul 3, 2008 at 7:28 AM, Dave Doty <ddoty@...> wrote:

When I run a Java program or a junit test in my grails project, the classpath is extensive. The following is a partial printing of the classpath when I run a JUnit test (from within Intellij):

***
E:\grails\lib\standard-2.3.jar
E:\grails\dist\grails-cli-1.0.3.jar
E:\grails\lib\spring-binding-2.0-m1.jar
... dozens more
***

However, I need access to some libraries in my project's lib subdirectory while my program is running as well. In particular, I run some junit tests from within the program, so I want access to junit.jar (placed in the lib directory). When I run grails run-app (either from the command line or from within Intellij) and print the classpath, it is rather stark:

*** 
e:/grails/lib/groovy-all-1.5.6.jar
e:/grails/dist/grails-cli-1.0.3.jar
***

I cannot determine how to include more jar files on the classpath when running grails run-app. I was under the impression that anything in the lib directory would be included, but that is obviously true only for compilation.

Dave

 



--
Dave




--
Dave




--
Dave
LightInTheBox - Buy quality products at wholesale price