LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-09-2011, 12:16 AM   #1
xuancong
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Rep: Reputation: 0
Question Will changes in Java binary executable affect running Java programs?


Hi,

I'm using Windows and Linux/Ubuntu. I want to know whether java virtual machine loads all dependent class binaries before executing the program. And if I changed the Java code and recompiled to form new class files, will it affect the outdated copy of the same Java program that are already running?
Take note that my code does not invoke explicit class loader (things analogous to Wind32 function LoadLibrary("???.dll")). And those parts that are changed and recompiled are dependent files at compile time.

And does this behaviour OS dependent? Thanks!

Xuancong
 
Old 04-10-2011, 07:43 AM   #2
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
It's been a while since i last used Java, but if i did not misread your question, this shouldn't be too hard to test. Why don't you write some simple class with a certain behavior (always returns "old", when a certain function is called for example), start a program calling that class and then try to replace the class with some new class (returning "new" instead). If the class is replaced successfully, the answer should change, otherwise it shouldn't.

regards, Heraton

Last edited by Heraton; 04-10-2011 at 07:44 AM. Reason: Bad grammar
 
Old 04-10-2011, 02:47 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Q: java virtual machine loads all dependent class binaries before executing the program?
A: No, not necessarily. Java can (and does) freely load classes as needed at runtime. The Java runtime is dynamic.

Quote:
Q: Take note that my code does not invoke explicit class loader... And those parts that are changed and recompiled are dependent files at compile time.
A: If it's a small enough program, it's likely that Java *will* load everything it needs at startup.


Quote:
this shouldn't be too hard to test. Why don't you write some simple class... start a program calling that class ... and then try to replace the class with some new class (returning "new" instead).
Good advice. Under most circumstances, you probably WON'T load the new version of the class.

Better advice: don't rely on this kind of behavior in a production program.

Here's a great, short article on the subject:

http://onjava.com/pub/a/onjava/2005/...ssloading.html

'Hope that helps .. PSM

PS:
There isn't just one class loader: there's actually a hierarchy. Another good link:
http://onjava.com/pub/a/onjava/2003/...assloader.html

Last edited by paulsm4; 04-10-2011 at 03:14 PM.
 
Old 04-12-2011, 05:31 AM   #4
xuancong
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by Heraton View Post
It's been a while since i last used Java, but if i did not misread your question, this shouldn't be too hard to test. Why don't you write some simple class with a certain behavior (always returns "old", when a certain function is called for example), start a program calling that class and then try to replace the class with some new class (returning "new" instead). If the class is replaced successfully, the answer should change, otherwise it shouldn't.
No. Not as easy as you think. Loading the actual .class byte-code file upon object instantiation (i.e. new ObjectClass(...)) is very inefficient. Imagine: everytime as you new something(...), JVM starts to load something.class from the disk. OMG: it'll jam your disk IO and make execution cumbersome. Therefore, any JVM implementor with IQ>0 will not do this.
So what I'm worried is that in windows, there's something called delayed loading: the .dll is loaded when the actual code/data is accessed. If the same thing happens to Java, testing a small piece of code may not imply the same conclusion on a very complex bundle of code involving thousands of classes (my case).

Thanks to http://java.sun.com/docs/books/jls/s...doc.html#47907
This means, as long as my Java code doesn't call LoadClass to dynamically load a class at runtime, once things gets running, .class files are free to change. Before codes gets started running, it's linked; before it's linked, it must be loaded which reads the .class bytecode file.

Xuancong
 
Old 04-13-2011, 01:37 PM   #5
Heraton
Member
 
Registered: Apr 2011
Location: Germany
Distribution: Mint 10, openSuSE
Posts: 58

Rep: Reputation: 3
Well, now i am sure i misread your question. I somehow expected the question to be more general, and therefor didn't realize you where looking for the details.

It's just curiosity, but i wonder what application needs to change the dependent classes at runtime? I can find only one reason: Tricking the JVM into running selfmodifying code...

regards, Heraton
 
Old 04-14-2011, 01:43 AM   #6
xuancong
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Wink

Quote:
Originally Posted by Heraton View Post
Well, now i am sure i misread your question. I somehow expected the question to be more general, and therefor didn't realize you where looking for the details.

It's just curiosity, but i wonder what application needs to change the dependent classes at runtime? I can find only one reason: Tricking the JVM into running selfmodifying code...

regards, Heraton
To honest, I'm a postgrad doing machine learning. I have several small ways of modifying the algorithm and just want to test whether it works. The experiment takes long time to run and duplicating the whole package for each small modification is clumsy. I just want some short cut-
 
Old 04-14-2011, 02:04 AM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Loading the actual .class byte-code file upon object instantiation (i.e. new ObjectClass(...)) is very inefficient. Imagine: everytime as you new something(...), JVM starts to load something.class from the disk.
Of COURSE that's inefficient. Of course Java doesn't do this.

Quote:
So what I'm worried is that in windows, there's something called delayed loading: the .dll is loaded when the actual code/data is accessed.
"Delayed loading" is a Good Thing

Quote:
This means, as long as my Java code doesn't call LoadClass to dynamically load a class at runtime, once things gets running, .class files are free to change.
If you believe this means that no Java program will ever load a class file once it's finished initializing - then you're mistaken
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[openbsd]Building and running Java programs in terminal. LinuxNewbie999 *BSD 1 09-03-2008 12:40 AM
running java based programs linuxhippy Slackware 8 01-23-2006 12:45 AM
Running a Java executable class from another executable class LUB997 Programming 22 07-24-2005 05:57 AM
Java SDK won't install, binary file not executable, yoper linux 2.6 kernel lopoetve Linux - Software 8 10-30-2004 04:03 AM
Problems on running java programs indro Linux - General 1 07-19-2001 12:34 PM


All times are GMT -5. The time now is 08:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration