Hi
i want to restart my program(a network simulator) after i check if the network has died in my program , so tha i dont have to hit the run button on eclipse. How can i do this
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi
i want to restart my program(a network simulator) after i check if the network has died in my program , so tha i dont have to hit the run button on eclipse. How can i do this
I have this code which one of my classmate is using ,but i dont know how should i use it ,it has .jar file in the syntax
try{
Runtime.getRuntime().exec("java -jar /Users/xyz/Desktop/Test/file.jar");
System.exit(0);
}
catch (Exception e){}
}
restarting the program
Duplicate post
Improving the world one idiot at a time!
First of all you people have two different addresses,and i dint know both of them point to one forum ,its my fault!
secondly atleast reply to a post,someone who needs your assistance is not here to hear you lame comments that its a duplicate/cross post..
i think the main goal of a forum should be to help solving problems ,instead of caring about these lil issues
jack_nutt,
Can i please bring your attention to a particular section of the forum guidance post,
Please note that we have rules against cross-posting, as do many other forums. Not only are you breaking ours, but you are breaking others.3b. Please post links to the sites you have cross-posted your question(s) on. This allows our members to join in on the discussion without having to ask the same questions over again. See The Problems With Cross Posting for greater details on this subject.
java-forums.org is not a memebr of our family, just many of our memebrs choose to be active members on multiple forums, so that they can provide solutions to more people, because its what they do.
Please take the time to read the post aboutthe problems with cross posting as linked to in the above qoutation. When you do this, you will realise why it is in your interest to abide by these rules.
Junky, is a very useful member of the community and provides help to many people.
The main goal of the forum is to provide solutions to people as well as a friendly environment, but people who end up wasting time posting replies to threads already solved on other forums cannot help many people.
Regards,
Chris
bgroenks96 (November 3rd, 2011), copeg (November 3rd, 2011), Tjstretch (November 10th, 2011)
try
I'm asuming in this example that your class name is netProgram.. which would give your first method a name ofsetVisible(false); // if statement to see network status.. // if the net status is true ... new netProgram(); setVisible(true); ...
public netProgram(){ }
Also I'd use a timer.. So make the program generate this code every say few mins.. to stop the processing usage..
But this is just a quick way of doing it
Perhaps this is nitpicky, but it also assists those trying to help you, if you take the time to utilize the SHIFT key for capitalization and SPACE key for spaces, as well as using full
English words rather than slang abbreviations. Not only does this promote a sense of professionalism with your issue, but it simply allows people to understand what you are
saying in a much simpler and faster fashion, rather than spending time that could be used on solving this issue on deciphering a mess of lazy typing.
Bad English is one thing, as those who aren't fluent simply can't help it, but lazy typing is just sort of rude when asking people to help solve a problem.
jack_nutt (November 10th, 2011)
Now to reply to the actual post:
There are two perfectly good methods to doing this. First, you can try what another person has already mentioned, which is restarting via a console command:
Runtime.getRuntime().exec(System.getProperty("user.dir")+[program file name here]); //Starts a new JVM running the same program. System.exit(0); //Terminates the currently running JVM
Alternatively, and probably more cleanly for your situation, you can keep a master while loop in the main thread, so that as long you nor the JVM have issued a termination command, the program will
always restart upon method completion unless told otherwise.
private static boolean running = true; public static void main(String[] args) { while(running) { //Do whatever } } //False if you want execution to halt upon the completion of your code. True if you want it to remain running and "restart" once your code has finished. public static void setStatus(boolean stat) { running = stat; }
Last edited by bgroenks96; November 3rd, 2011 at 09:42 PM. Reason: Fixed code tags and added information.
jack_nutt (November 10th, 2011)
import java.io.*; public class a { public static void main(String[] args) { for(int i=0;i<50;i++) { try { BufferedWriter meanValue = new BufferedWriter(new FileWriter(new File("filename.txt"), true)); meanValue.write(""+i); meanValue.newLine(); meanValue.close(); } catch (Exception e) { } System.out.println(i); if(i>10){ try{ Runtime.getRuntime().exec("C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar"); System.exit(0); } catch (Exception e){} } } } }
I have created a .jar file at location C:\Users\abc\workspace\crejrfiles\lib\jarf.jar ,using eclipse
But the program runs only once ,how can i make it to keep on restarting so that i keep on logging values in my text file.. what paramaters should i give to
Runtime.getRuntime().exec (...............)
You need to put in the syntax you would need to call the program as if you were doing it from the command line.
To run a java program from the command line:
On windows systems, you will either have to manually type in the path to the java executable, or add it to your system path variable.java -jar jarf.jar
Ex.:
Say I have java.exe located at C:/Program Files/java/jre7/bin
The the command would be:
You'll need to format this command to fit inside of a Java string, then execute it from Runtime.getRuntime().exec()"C:/Program Files/java/jre7/bin/java -jar C:/some_jar.jar"
I tried this in Windows
Runtime.getRuntime().exec("java -jar C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar");
But its not working ...
my jarfile is at location "C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf. jar"
what should i do?
Define "not working".
Did you add the java executable path to your Windows system path?
By your description i tried the command
Runtime.getRuntime().exec("C:\\Program Files\\Java\\jre6\\bin\\java -jar C:\\Users\\abc\\workspace\\crejrfiles\\lib\\jarf.jar");
As my jave files are in folder C:\Program Files\Java\jre6\bin
Not working means
i want my program(process) to keep on restarting and running in backhand ...I am actually logging a file in my program and i want the simulation to run automatically everytime.
Is the JAR launching properly? If the JAR you are launching is the same one executing this command, it should continuously keep spawning a new process with each execution. Just be careful to set that up correctly. You don't want to inadvertently fork bomb your computer...
No i think my jar file is not working, when ever i click on it ,it gives an error
"invalid or corrupt jar file C:\Users\abc\workspace\project\lib\jarf.jar "
how can i do it in Eclipse? what options do i need to consider while exporting this jar file
how can i get rid of this error??