1

I have the following class :

After compiling my CreateTexts.java class, I want to run it via getRuntime (calling it through Eclipse ). So I run this class

public class RuntimeDemo { public static void main(String[] args) { try { // create a new array of 2 strings String[] cmdArray = new String[2]; // first argument is the program we want to open //C:\Program Files\Java\jdk1.6.0_20\bin cmdArray[0] = "C://Program Files//Java//jdk1.6.0_20//bin//java"; // second argument is a txt file we want to open with notepad cmdArray[1] = "C://logback//CreateTexts"; // create a process and execute cmdArray and currect environment Process process = Runtime.getRuntime().exec(cmdArray,null); } catch (Exception ex) { ex.printStackTrace(); } } } 

But I'm not seeing the output file being created. Any tips appareciated thanks

2
  • i believ i need to use ProcessBuilder class , not this way Commented Jul 10, 2015 at 3:04
  • Why not simply call batch file in jthis code, let the batch file do the job of running your CreateTexts code? Commented Jul 10, 2015 at 3:43

1 Answer 1

1

The way you are running the program is wrong.

Let's suppose you CreateTexts is inside the package logback. And the file is located in "C://logback//CreateTexts", then you have to cd to C: and then execute the program as java logback.CreateTexts.

Just directly calling the file using absolute path from any directory wont work.

Sign up to request clarification or add additional context in comments.

4 Comments

hmm, but how come I was able to compile it from Eclipse (which is in some random dir. ) ?
or i'm guessing thats a java security mechanism or smth ?
@Coffee, yes.. Instead of cding to the directory.. You can also add it to the classpath as mentioned in the answer in link...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.