Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes using 1.5 classes in 1.4? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using 1.5 classes in 1.4?" Watch "using 1.5 classes in 1.4?" New topic
Author

using 1.5 classes in 1.4?

AnthonyA James
Greenhorn

Joined: Jul 26, 2007
Posts: 5
Hello All,

If I write a program using Java 1.5, and say it uses class X that was introduced in Java 1.5 and does not exist in 1.4. If I had to run my program on a machine using 1.4, how would I do it? Can I simply copy the .class file for this class to the classpath and will it all work?

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

It would not work. Assuming that you used the "-source" and "-target" switches on the compiler to create a class file compatible with Java 1.4, it would still not work, you would get a NoClassDefFoundError when your program tries to use a class that doesn't exist in Java 1.4.

If you really want to do this for some reason, have a look at Retroweaver.
[ July 26, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Simply copying the .class file from JDK 1.5 in the 1.4 runtime would not work as class files are stamped with version number.

So if a class is compiled using JDK 1.4 ,then it would run well on 1.5 , but not the reverse.


Rahul Bhattacharjee
LinkedIn - Blog
AnthonyA James
Greenhorn

Joined: Jul 26, 2007
Posts: 5
Thank you for the helpful replies.
AnthonyA James
Greenhorn

Joined: Jul 26, 2007
Posts: 5
Am I right that the .class file is specific for each platform? So if I was to copy Enumeration.class from a x86 to a UNIX box, it would not work?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by AnthonyA James:
Am I right that the .class file is specific for each platform? So if I was to copy Enumeration.class from a x86 to a UNIX box, it would not work?


.class files are completely portable: there is absolutely no difference between Enumeration.class on a Windows machine or a Linux machine or a Mac or anything else.

But .class files do contain a version number, and with some major Java releases, Sun bumps that number. Generally, newer JVMs read all old class file versions, but the reverse is not true. By default, Foo.class compiled by JDK 1.5 will not work on a JDK 1.4 JVM; you'll see an error message about the class file version number.


[Jess in Action][AskingGoodQuestions]
Shoumin Li
Ranch Hand

Joined: Jul 06, 2007
Posts: 85
It' will not work,
If you want use the 1.5 classes you need rewrite it in the 1.4.


Java Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
You can create Java 1.4-compatible class files with a Java 5 compiler, though. Check out the "-source" and "-target" switches of javac. That won't help with the new classes in Java 5, though.


Ping & DNS - my free Android networking tools app
AnthonyA James
Greenhorn

Joined: Jul 26, 2007
Posts: 5
Originally posted by Ernest Friedman-Hill:


.class files are completely portable: there is absolutely no difference between Enumeration.class on a Windows machine or a Linux machine or a Mac or anything else.

But .class files do contain a version number, and with some major Java releases, Sun bumps that number. Generally, newer JVMs read all old class file versions, but the reverse is not true. By default, Foo.class compiled by JDK 1.5 will not work on a JDK 1.4 JVM; you'll see an error message about the class file version number.



Thanks for your response.

I was reading the following and this is what is confusing me:

The class files of the Java API are inherently specific to the host platform. The API's functionality must be implemented expressly for a particular platform before that platform can host Java programs. To access the native resources of the host, the Java API calls native methods. As you can see in Figure 1-6, the class files of the Java API invoke native methods so your Java program doesn't have to. In this manner, the Java API's class files provide a Java program with a standard, platform-independent interface to the underlying host. To the Java program, the Java API looks the same and behaves predictably no matter what platform happens to be underneath. Precisely because the Java virtual machine and Java API are implemented specifically for each particular host platform, Java programs themselves can be platform independent

Any insight would be appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
Originally posted by AnthonyA James:
The class files of the Java API are inherently specific to the host platform.


That's the important part. The paragraph not talking about class files in general, it's talking about the class files that implement the Java API for one particular platform. Those will indeed not be portable across platforms, but they don't need to, because every JVM has its own set.

Application class files (like the ones generated from your code) can be used on any platform, provided they don't do any platform-specific stuff themselves.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't know where this exceprt is from, or if there's missing context, but otherwise I'd say this is very misleading. There are a very few .class files in the API which are platform-specific; virtually all of them are the same in every JDK. Almost all platform dependencies are handled by different implementations of native methods used by those classes -- for example, the java.lang.Thread class is identical across platforms, but its native method code varies wildly. Most of the classes that have platform-specific implementations are in the AWT, things like package-private subclasses of java.awt.Graphics (note that Graphics is an abstract class which has no public subclasses!)
AnthonyA James
Greenhorn

Joined: Jul 26, 2007
Posts: 5
Maybe I'll understand things better if someone could explain the following to me.

When we call System.out.println("Hello World"), I am trying to figure out exactly what happens.

Now, obviously the System.class file has this method. And this method will call some native method on the OS that will do the printing out.
Now say this native method might be called prnt on Windows, and letsPrint on UNIX. Does this mean that in the System.class file for Windows it'll contain a call to prnt, and in the System.class file for UNIX, it'll contain a call to letsPrint? i.e. these values will be hardcoded into the .clas files, and hence they'll be different?

Thanks.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I do not know what exactly happens.

But the class file are platform independent and at some point of execution when we call native methods then the call gets handled by operating system specific libraries.Those will be different for different operating systems.

If you are interested to know as which are the classes the runtime loads during the process to run a file then you might try -verbose option while executing the program.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

Originally posted by AnthonyA James:
Maybe I'll understand things better if someone could explain the following to me.

When we call System.out.println("Hello World"), I am trying to figure out exactly what happens.

Now, obviously the System.class file has this method. And this method will call some native method on the OS that will do the printing out.
Now say this native method might be called prnt on Windows, and letsPrint on UNIX. Does this mean that in the System.class file for Windows it'll contain a call to prnt, and in the System.class file for UNIX, it'll contain a call to letsPrint? i.e. these values will be hardcoded into the .clas files, and hence they'll be different?

Thanks.

The method println(...) in your example is not in class System. Note that you are referring to the variable named 'out' of class System, which is a PrintStream. So you are calling the println(...) method in class PrintStream.

As Ernest already said, the class files for the different platforms are exactly the same. The difference is not in the class files, but in the implementation of the native methods that those classes are calling.

Let's look at another example, because class PrintStream doesn't contain any native methods (I've looked up the source code). Look at the method currentTimeMillis() in class System. If you lookup the source code for that method (you can find it in src.zip in the directory where you installed the JDK), you will see this:

public static native long currentTimeMillis();

Note the 'native' keyword. That indicates that the method is not implemented in Java, but in native, platform-specific code somewhere. Ofcourse that native code will be different on each platform; the Java class files however remain the same.

Calling native methods is done through JNI (Java Native Interface).
[ July 27, 2007: Message edited by: Jesper Young ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using 1.5 classes in 1.4?