• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on java's platform independency and native methods  RSS feed

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a query on how java programs are platform independent.
Literature available states that a java program calls JAVA API to use machine resources which
in turn calls native methods. As java code has only to interact with java platform(API+JVM)
which are available for different platforms, so java program runs on all platforms.

I am not able to understand how it happens.
Suppose I have to write a java program on windows platform.So I go ahead and install windows version of
jdk on my windows machine. So this jdk version is combintation of: JVM for windows+Java API for windows+native methods for windows.
Now in my code I call a JAVA API method which in turn calls native method. Now this native method is specific to windows ,
hence making my compiled program specific to windows platform. So how come my program becomes platform independent?
Please suggest where is my understanding of the whole picture wrong.

Manish
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:Now this native method is specific to windows ,
hence making my compiled program specific to windows platform.

Here is where your logic fails. Your code calls the platform-agnostic Java API -- how does that make your code specific to Windows?
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is where your logic fails. Your code calls the platform-agnostic Java API -- how does that make your code specific to Windows?


I think my understanding of how an entire program is organized/linked is wrong.

When whole program is compiled, doesn't it take into account entire method call chain?
I mean that doesn't native method also becomes a part of entire program, hence rendering it platform specific.

Manish
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java.exe is platform dependent - the classes are not (if they do not have their own native methods).

It is of course possible to make assumptions that make your program unportable.

Directory path delimiters and the usual end-of-line marking are different.

Typically a Unix platform will let an open file be reopened, renamed or removed, whereas Windows won't.
If your program depends on either way of behavior, it will suck on the other platform.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:
I think my understanding of how an entire program is organized/linked is wrong.

When whole program is compiled, doesn't it take into account entire method call chain?
I mean that doesn't native method also becomes a part of entire program, hence rendering it platform specific.

Manish



What you are describing hasn't been necessary since before Java. Even languages like C/C++ can dynamically load libraries at runtime -- and have been doing so for decades. In Unix, they are called shared libraries. And in Windows, they are called DLLs. etc. Granted, C/C++ has tons of other reasons that make them platform dependent, but any dependencies due to linking all of its dependent libraries isn't one of them.

Java goes even further than that, it not only doesn't statically link any libraries, it doesn't statically link any other class files as well. It merely uses the dependent class and jar files to figure out the interfaces, any actual linking (assuming that the terminology even applies here) is done at runtime by the JVM.

Henry
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a tons to all for clarifying my doubts.

Java goes even further than that, it not only doesn't statically link any libraries, it doesn't statically link any other class files as well.


Henry, this statement of yours answered my query very well, but it raised new question...though in different direction

Here you have stated in a way as if a class file and a library are quite different animals. Isn't a library merely a collection of
class files in java language?

Manish
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manish ghildiyal wrote:Thanks a tons to all for clarifying my doubts.

Java goes even further than that, it not only doesn't statically link any libraries, it doesn't statically link any other class files as well.


Henry, this statement of yours answered my query very well, but it raised new question...though in different direction

Here you have stated in a way as if a class file and a library are quite different animals. Isn't a library merely a collection of
class files in java language?


Manish



Well. that's true in other languages as well. An .obj file holds the binary for one source file, while a .DLL (for windows) or .so (for Unix) holds lots of object binaries (along with details about them).

And my comment wasn't comparing them to each other, it was comparing them to their counterparts(*). For example, in windows, DLLs are libraries that can be dynamically loaded. There is no equivalent for object files in windows -- single object files are not dynamically loaded..

Henry


* again, assuming that comparing an actual environment to a virtual environment is even viable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!