import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class TestExecWindows
{

	public static void main(String[] args)
	{	
		File testDir = new File("test&me" + File.separator);
		
		if (!testDir.exists()) {
			testDir.mkdirs();
        }
		
		File testFile = null;
		try
		{
			testFile = File.createTempFile("test", ".bat", testDir);
		}
		catch(IOException e)
		{
			e.printStackTrace();
			return;
		}
		
        try
		{
        	final FileWriter fout = new FileWriter(testFile);
			fout.write("echo off" + System.lineSeparator());
			fout.close();
		}
		catch(IOException e1)
		{
			e1.printStackTrace();
		}
		
		String canonicalPath = null;
		try
		{
			canonicalPath = testFile.getCanonicalPath();
		}
		catch(IOException e)
		{
			e.printStackTrace();
			return;
		}
		
		testExec(canonicalPath);
		testExec("\"" + canonicalPath + "\"");
	}
	
	private static boolean testExec(String execCommand)
	{
		Process pr = null;
		try
		{
			System.out.println("Command: " + execCommand);
			pr = Runtime.getRuntime().exec(execCommand);
			pr.waitFor();
		}
		catch(IOException e)
		{
			e.printStackTrace();
			return false;
		}
		catch(InterruptedException e)
		{
			e.printStackTrace();
			return false;
		}
        
        int res = pr.exitValue();
        
        System.out.println("Result: " + res);
        
        return res == 0;
	}

}
