[groovy-user] groovyc javac ant task

24 views
Skip to first unread message

Pavel Sapozhnikov

unread,
Jun 24, 2010, 10:36:35 AM6/24/10
to us...@groovy.codehaus.org
Hi all

I am trying to compile my groovy code to be used in java 1.4. This is a snippet of my build.xml

<target name="compile" description="Compiles the source" depends="clean">
        <groovyc srcdir="src" destdir="build" failonerror="true" verbose="true">
            <javac source="1.6" target="1.4" debug="on"/>
        </groovyc>
</target>

After that I jar the classes. The machine where this gets compiled is running java 1.6 which is why I set source=1.6 and target is 1.4. Is there something that I am missing here because still I get following exception:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/... (Unsupported major.minor version 49.0)
        at java.lang.ClassLoader.defineClass0(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$100(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)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)

Is it something that I am doing in that class which cannot be compiled down to 1.4 and so it compiles it to 1.5? Groovy that I am using is 1.6.9 jdk 1.4 version.

-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com

Roshan Dawrani

unread,
Jun 24, 2010, 3:06:57 PM6/24/10
to us...@groovy.codehaus.org
I think the target that you set on <javac> inside <groovyc>, Groovyc uses only for joint compilation. So it will be used only for the java classes that joint compiler compiles and not for groovy classes. Maybe someone who is better aware of it can confirm / add more.

I didn't dig deep but as far as I saw, I didn't see Groovyc passing target to Javac even for joint compilation.

-- Roshan

Pavel Sapozhnikov

unread,
Jun 24, 2010, 3:13:51 PM6/24/10
to us...@groovy.codehaus.org
Thank you Roshan. Is there a way to compile .groovy with javac ant task and not groovyc ant task?

-p


-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com


Roshan Dawrani

unread,
Jun 24, 2010, 3:18:27 PM6/24/10
to us...@groovy.codehaus.org
No, javac doesn't know anything about groovy syntax and what to do with it.

groovyc launches groovy's own compiler that knows about groovy language and how to generate the bytecode valid for the JVM.

So, *.groovy has to be compiled with groovyc task only, but I am not aware of any "target" like option that it provides.

Pavel Sapozhnikov

unread,
Jun 24, 2010, 3:24:06 PM6/24/10
to us...@groovy.codehaus.org
Understand thank you. And groovyc doesn't have any way of telling it to compile all groovy files with respect to 1.4?


-p
-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com


Roshan Dawrani

unread,
Jun 24, 2010, 3:41:15 PM6/24/10
to us...@groovy.codehaus.org
I don't really see (yet) any configuration options coming from <groovyc> to groovy compiler to control the target bytecode version.

At the class/bytecode generation time - if your groovy code does not use generics (not only directly but also indirectly - say if ur superclass uses generics, then ur class is deemed to be using generics too), annotations, then groovy compiler generates 1.4 compatible code, otherwise 1.5 compatible.

That's as far as I have been able to see yet.

Is there any class for which you think you are not using generics/annotations(directly / indirectly) but groovy is still generating 1.4 incompatible code?

-- Roshan

Pavel Sapozhnikov

unread,
Jun 24, 2010, 3:51:38 PM6/24/10
to us...@groovy.codehaus.org
Basically as far as I know I get this particular exceptions on 2 classes of mine and I didn't know that I'm using generics anywhere. Maybe I am?

For instance I have code like this:

def resultSet = []
sql.query("{call autoRpt_something ()}", []){ rs ->
                while(rs.next()){
                    resultSet.add(['clientkey':rs.getInt('ClientKey'), 'requestId':rs.getInt('RequestId'), 'reportId':rs.getInt('ReportId'),
                    'emailAddress':rs.getString('EmailAddress'), 'entityKey':rs.getInt('EntityKey'), 'reportName':rs.getString('ReportName'),
                    'fromDate':sf.format(rs.getDate('FromDate')), 'toDate':sf.format(rs.getDate('ToDate')), 'frequency':rs.getString('Frequency'),
                    'activeUsersOnly':rs.getInt('ActiveUsersOnly'), 'excludeZeroActivity':rs.getInt('ExcludeZeroActivity'), 'id':rs.getString('Id'),
                    'level':rs.getString('Level'), 'userid':rs.getString('UserId')])
                }
            }
return resultSet

and also

def result = [:]
            sql.query("{call something (?,?)}", [entityKey, reportName]){rs ->
                while(rs.next()){
                    result.put('clientName', rs.getString('ClientName'))
                    result.put('entityName', rs.getString('EntityName'))
                }
            }
           
            return result


Is this generics? At least I don't think so. To me this seems like some primitive arrays and maps. In the other class I have lots of regular expressions and I know java 1.4 supports regular expressions - well except my regular expressions are in groovy way.


-p

-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com


Pavel Sapozhnikov

unread,
Jun 24, 2010, 3:54:34 PM6/24/10
to us...@groovy.codehaus.org
By the way if my code has apparently generics or annotations wouldn't it fail if machine had already java 1.4. Would it just not compile throw me exceptions all over the place. I did compile this on a machine which has java 1.4 and it did work. My build machine has I think later than 1.4.


-p

-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com


Roshan Dawrani

unread,
Jun 24, 2010, 3:57:00 PM6/24/10
to us...@groovy.codehaus.org
Not in the bits you have shown but if your class is defined as

class Test<E> {....} or class Test extends ArrayList {...} (where ArrayList uses generics), then u use it.

So, depends on the full class definition..

Pavel Sapozhnikov

unread,
Jun 24, 2010, 3:59:28 PM6/24/10
to us...@groovy.codehaus.org
Here's an example class definition:

class DBUtils {
    def connectionURL
    def userName
    def password
    def driverClassName
    def sql
    def dateFormat
   
    static final log = Logger.getLogger(DBUtils.class)
   
    private DBUtils(){
        ...
           
        }catch(FileNotFoundException e){
            log.debug(e)
            throw new FileNotFoundException('File automatedReporting.properties was not found')
        }finally{
            if(fs){
                fs.close()
            }
        }
    }
    private static DBUtils dbUtils
   
    def static DBUtils getInstance(){
        if(!dbUtils){
            dbUtils = new DBUtils()
        }
        return dbUtils

    }
-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com


Roshan Dawrani

unread,
Jun 24, 2010, 4:06:25 PM6/24/10
to us...@groovy.codehaus.org
It takes a few things into consideration

*  check whether generics / annotations is being used or not - say using_java5features = y/n

* the bytecode version configured on CompilerConfiguration (the default here is taken here by whether it is able to see "java.lang.annotation.Annotation" class or not)

Now,

1) if using_java5features = NO, it generates 1.4 compatible classes

2) if using_java5features = Yes, but you are on a JDK 1.4 machine - I guess ASM library ignores all java 5 stuff and generates 1.4 compatible code - this is why ur code compiled on a machine having 1.4 works.

3) if using_java5features = Yes, but you are on a JDK 1.5/1.6 machine (i.e. it can see "java.lang.annotation.Annotation" class), then it generates 1.5 compatible code.

Now, for 2) and 3) I see options in CompilerConfiguration for overriding the default, but I don't see it coming from <groovyc>.

I hope I am making sense still..

-- Roshan

Pavel Sapozhnikov

unread,
Jun 24, 2010, 4:08:22 PM6/24/10
to us...@groovy.codehaus.org
That's a good idea. Can you elaborate? How do I use this command?


-----------------------------------------------------------------------
Pavel Sapozhnikov
Software Engineer
Gryphon Networks
781-551-3216
psapoz...@gryphonnetworks.com


Reply all
Reply to author
Forward
0 new messages