• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

running a java program

 
Prahlad Joshi
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to run a package containing .class files from a java program for this i have written:
Process tempProcess=Runtime.getRuntime().exec("java "+ mainClassName,null,new java.io.File(absolutePackagePath));
tempProcess.waitFor();
but the problem is that i am not able to given input to this tempProcess or see output of it.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code may produce some output or need some input that your code isn't handling. See this article for pitfalls to avoid when using Runtime.exec.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from the issues that Ulf pointed out, are you also sure you want to start a new JVM to run your sub-application? Depending on exactly what you want to achieve, you may find the Reflection API more useful and appropriate. Things get slightly complicated if the .class files aren't already in your current classpath (you may need to create a new class loader), but this may still be better than using Runtime.exec().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic