• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java compiler generate older versioned byte code?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have Java 5 source code. I want to compile it so that it will run in a Java 1.3 JVM. The project I'm working on uses Ant so I've included the following in the ant script:

<javac srcdir="${project_src.dir}" debug="on" failonerror="${haltonerror}"
destdir="${project_classes.dir}"
source = "1.5"
target = "1.3"
includes="com/company/project/**/*">
<classpath refid="classpath" />
</javac>

When I run the ant script I get the error:

[javac] javac: source release 1.5 requires target release 1.5

I thought the whole point in the source and target parameters was that the compiler was smart enough to strip out the extra 1.5 code and generate 1.3 byte code. Is this not the case? Must the target version always be the same as or greater than the source version?

Note: as this isn't an ant query, more a compiler query, I have posted it in the Java in General forum.

Thanks in advance for any replies.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thought the whole point in the source and target parameters was that the compiler was smart enough to strip out the extra 1.5 code and generate 1.3 byte code.


The compiler can't possibly strip out code, since then you'd be left with a non-functional program. The good news is that -despite setting the source level to 1.3- the code will still be compiled against the 1.5 class libraries, so you can use the newer classes and methods. But since the compiler now generates class files that can run on 1.3, your code needs to check the version at runtime, and make sure it doesn't call 1.4 and 5 methods when running on 1.3.
 
Kevin Kilbane
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf, thanks for the reply.

The compiler can't possibly strip out code, since then you'd be left with a non-functional program


Where I said I thought the compiler would strip out the 1.5 code, I really meant convert it e.g.



would be converted to something like



but it looks like this is not the case.

Anyway, I think what you are saying is that I can only use the source = "1.3" parameter where the source code does not actually include any of the new syntax or APIs introduced in 1.4 or 1.5 - is that right?

But since the compiler now generates class files that can run on 1.3, your code needs to check the version at runtime, and make sure it doesn't call 1.4 and 5 methods when running on 1.3.


Not sure what you mean here - if the code calls 1.5 methods then surely it wouldn't compile in the first place when a source of 1.3 is specified?

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to convert byte code you can use retroweaver/retrotranslator.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think what you are saying is that I can only use the source = "1.3" parameter where the source code does not actually include any of the new syntax or APIs introduced in 1.4 or 1.5 - is that right?


Not quite. With source="1.3" it can't contain newer syntax (like generics or the enhanced for loop). But it can contain calls to Java 5-only methods and classes.

if the code calls 1.5 methods then surely it wouldn't compile in the first place when a source of 1.3 is specified?


No, the compiler still uses the Java 5 class libraries with its new methods and classes, so it would compile just fine.
Some desktop applications get compiled (say) to run on Java 5, yet will take advantage of Java 6 features where they are available. In order to do that they examine the Java version at runtime (by checking the "java.specification.version" system property) and use that to determine whether to run the Java 5 code or the Java 6 code. The result is an application that runs on a wider range of machines, yet with possibly some non-essential features missing; it's called "degrading gracefully".
 
reply
    Bookmark Topic Watch Topic
  • New Topic