This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I got a simple question about procces of java to ask  RSS feed

 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can Java create a new process in windows? as VC if i got a main code to run and invoke another .exe file. That means you just create a new procces has nothing to do with the main code. but VC on fit for windows.
Java run on the JVM which is a process of windows,i want to use java to create a new java process. Because i got a task to program a online update module. this is the request:
1.the update package is a .jar file
2.in this whole update procces, the main code can not stop. that mean you must keep the main code runing.
3.you just can only stop some thread you want to update
4.then cover class refer the thread with new class(the latest version)
5.then you just start the thread.
6. this module also fit for linux. So i can not invoke .dll with java to finish the task. In that case, this code can only fit for windows
So I just want to use "Runtime.getRuntime().exec(Command)" to finish it. But i don't know whether this method can finish this task.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can start a new process by using Runtime.getRuntime().exec(...), or by using class ProcessBuilder (if you're using Java 5 or newer).
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you!
but i get another question, whether i can cover some class dynamicly when my java program is runing? that's the point i care about.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by "cover". Do you want to reload an updated version of some class without restarting the application?
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a idea like this:
create a thread to run a empty function

public class app1 {
public void out(){

}
public void execute(){
}
out is the empty function,execute is check for update class function

}
then create another class for update. this is not a empty class

public class app1 {
public void out(){
System.out.println("test");
}

}
create a thread to run this empty class
class start extends Thread {

public void run(){
while(true){

app1 a=new app1();
a.out();
a.execute();
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}


}
}

when execute find a class named app1.class in a specified folder then
stope the thread and use
Runtime().exec("copy xxx.class to xxx.class") to update app1.class
do you think this idea can success?
i'm sorry for asking this simple question,because i lack knowledge in this field.
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to stan jamas:
I'm not sure what you mean by "cover". Do you want to reload an updated version of some class without restarting the application?

i think i don't descrbe this problem clearly, because my english is not very good,sorry. conver means copy. when the application/process is runing i must copy this some new class to cover the old class by using copy command.
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first, i think the windows will lock the process.but i think i'm wrong,i just cover the old class without stopping its thread, it works. but the program still output the old class result, but i think if i restart this thread maybe it works.
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get a test like this:
i get a app1.class for two version
old verison:
public class app1 implements update {
public void out(){
System.out.println("2");
}
}
new version:
public class app1 implements update {
public void out(){
System.out.println("1");
}
}
then create a new thread to run old version,then copy the new version to overwrite the old version after i stop the thread,then i create a new class and a new thread to run the app1. I find the output is the old version's result.
that means when you start a jvm all the class is loaded to the jvm's memroy? so you create a new class or create a new thread, that is only you create a class from the jvm memory. If you want to get the new version result you must restart your jvm?
who can give me the answers? right or wrong? But i just want to get the new version result without restarting jvm. i just only want to restart the thread.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do some tricks with class loaders to reload a class. So you might be able to use exec or the new compiler APIs in Java6 to compile the class, then reload it. The problems are any references to objects created from the old class won't be updated and won't be compatible, and if you don't clear all references to the old class and the old class loader they won't be garbage collected so repeated reloads could eat up memory.

I learned how to do this by reading the code for JUnit - the GUI version reloads the test and the class under test every time it runs the test. Then I never used it.

(Note: JUnit might use a separate process nowadays ... it's been a while since I looked into its loader.)
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thaks your reply. sorry i can't get it. you mean jdk5.0 does not have this function? if i use api of java 6, i can finish it? or use exec in jdk5.0?but i don't know how to get it. i have a experiment, it represent that the jvm load the classes to the memory.When i get a thread run then delete the related class and restart the thread. i see it still work.So i get that result. So i think if i want to reload the classes i must restart the jvm process.can you give me some detail point how the junit get it? i don't know whether i make you understand.
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i get a mistake.i asked our manager, he said you can stopped the process(we called application procces),but beforce you stopped you must started a update-process to copy the class to replace the old class then restarted the application-process before you stopped the update-process.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!