On 19/01/10 11:52 PM, Craig Wickesser wrote:
I tried #3 but that gave me an error about another dependency (org.objectweb.asm?? something like that).

Sounds like you didn't include all the jars in the -cp argument. Using -cp *.jar as John suggested should work.

For option #2, how do I get gradle to build the class-path for me so I don't have to manually copy/past JAR names in my gradle script?

You can do something like:

jar.doFirst {
jar.manifest.mainAttributes('Class-Path': configurations.runtime.collect { it.name }.join(' '))
}



On Mon, Jan 18, 2010 at 9:11 PM, Adam Murdoch <[email protected] <mailto:[email protected]>> wrote:



    On 19/01/10 12:48 PM, Craig Wickesser wrote:
    So I added the following to my build.gradle:

        task copyToLib(dependsOn:
        configurations.default.buildArtifacts, type: Copy) {

            into('build/output/lib')

            from configurations.default

            from configurations.default.allArtifacts*.file

        }


    Then I ran:
    $ gradle build
    $ gradle copyToLib

    Next I tried to run the JAR...
    $ cd build\output\lib
    $ dir

     Directory of C:\work\projects\playground\mrhakibook\build\output\lib

    01/18/2010  08:41 PM <DIR>          .
    01/18/2010  08:41 PM <DIR>          ..
    07/09/2008  03:04 PM         1,323,005 ant-1.7.1.jar
    07/09/2008  03:04 PM            12,143 ant-launcher-1.7.1.jar
    01/13/2007  01:28 AM           445,288 antlr-2.7.7.jar
    08/27/2006  08:49 PM            34,832 asm-2.2.3.jar
    08/27/2006  08:49 PM            17,977 asm-analysis-2.2.3.jar
    08/27/2006  08:49 PM            16,248 asm-tree-2.2.3.jar
    08/27/2006  08:49 PM            34,989 asm-util-2.2.3.jar
    12/02/2009  07:11 AM         3,987,117 groovy-1.6.7.jar
    02/22/2008  02:53 PM            87,325 jline-0.9.94.jar
    03/03/2006  06:22 PM           120,640 junit-3.8.2.jar
    01/18/2010  08:41 PM            30,589 foo-1.0-SNAPSHOT.jar

    $ java -jar foo-1.0-SNAPSHOT.jar

    and I get..

    Exception in thread "main" java.lang.NoClassDefFoundError:
    groovy/lang/GroovyObject
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown
    Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
            ... 12 more
    Could not find the main class: org.mindscratch.foo.ParseMain.
    Program will exit.

    I'm not sure why it can't find "GroovyObject"...the JAR is in the
    same directory as my "foo" jar.

    I tried this as well:

    $ java -cp . org.mindscratch.foo.ParseMain     ....and I get:

    Exception in thread "main" java.lang.NoClassDefFoundError:
    org/mindscratch/foo/ParseMain
    Caused by: java.lang.ClassNotFoundException:
    org.mindscratch.foo.ParseMain
            at java.net.URLClassLoader$1.run(Unknown Source)

    I did verify the ParseMain class does exist in my "foo" jar.


    By using -cp . you're asking the jvm to look for class files in
    the current directory. This doesn'twork because the classes are
    all packaged into jars. Instead, you want to ask it to look for
    class files in each of the jars in the current directory.  Some
    ways you can do this:

    1. Include each of the jar names in the argument to the -cp
    option, ie java -cp foo-1.0-SNAPSHOT.jar:groovy-1.6.7.jar:...

    2. Include a 'Class-Path' attribute in the manifest of your jar
    which lists all of the jars, then you can run java -jar
    foo-1.0-SNAPSHOT.jar

    3. Merge all of the jars together, as described in the cookbook,
    then you can run java -jar foo-1.0-SNAPSHOT.jar



    Thanks in advance.
    craig

    On Sun, Jan 17, 2010 at 6:27 PM, Adam Murdoch
    <[email protected] <mailto:[email protected]>> wrote:



        On 18/01/10 4:15 AM, Craig Wickesser wrote:
        I'm trying to get an executable JAR that contains groovy
        code that I can run by doing a simple: java -jar myapp.jar

        Here's my build.gradle

        usePlugin 'groovy'
        usePlugin 'maven'  // Maven plugin to install artifact in
        local Maven repo.

        sourceCompatibility = '1.6'
        targetCompatibility = '1.6'

        manifest.mainAttributes("Main-Class" :
        "org.mindscratch.foo.ParseMain")

        def localMavenRepo = 'file://' + new
        File(System.getProperty('user.home'),
        '.m2/repository').absolutePath
        repositories {
            // Use local Maven repo location. We don't need this if
        we only want to install
            // an artifact, but we do need it if we want to use
        dependencies from the local
            // repository.
            mavenRepo urls: localMavenRepo
        }

        // Project configuration:
        version = '1.0-SNAPSHOT'
        group = 'org.mindscratch.foo'

        // The following line is not necessary. Default the install
        tasks depends on the
        // jar task, but this means no tests and checks are executed
        when we use the
        // install task. The following line makes the install tasks
        depend on the build task
        // and now all tests and checks are done before install is
        executed.
        install.dependsOn ':build'


        repositories {
            mavenCentral()  // Define Maven central repository to
        look for dependencies.
        }

        dependencies {
            groovy 'org.codehaus.groovy:groovy:1.6.7'  //
        group:name:version is a nice shortcut notation for dependencies.
            testCompile 'junit:junit:4.7'
        }


        task initProject(description: 'Initialize project directory
        structure.') << {
            // Default package to be created in each src dir.
            def defaultPackage = 'org/mindscratch/foo'
            ['java', 'groovy', 'resources'].each {
                // convention.sourceSets contains the directory
        structure
                // for our Groovy project. So we use this struture
                // and make a directory for each node.
                convention.sourceSets.all."${it}".srcDirs*.each { dir ->
                    def newDir = new File(dir, defaultPackage)
        logger.info <http://logger.info> "Creating directory
        $newDir"  // gradle -i shows this message.
                    newDir.mkdirs()  // Create dir.
                }
            }
        }


        I run:
        $ gradle install -i
        $ cd build/libs
        $ java -jar foo-1.0-SNAPSHOT.jar

        ..doesn't work b/c the Groovy jar isn't on the classpath (or
        inside my JAR).  GUess what I need is a JAR that contians
        the Groovy jar inside of it (maybe in a "lib" folder).  Can
        you help?


        The cookbook has some options for creating a JAR which
        contains its compile or runtime dependencies:
        
http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar

        There's also a JIRA issue for making this easier:
        http://jira.codehaus.org/browse/GRADLE-566


-- Adam Murdoch
        Gradle Developer
        http://www.gradle.org


-- Adam Murdoch
    Gradle Developer
    http://www.gradle.org


--
Adam Murdoch
Gradle Developer
http://www.gradle.org

Reply via email to