Susha Naidu
Susha Naidu

Reputation: 307

Unable to run batch commands using java class

I have created a batch file with the following commands:

@echo off echo.>"Desktop:\testing\draft.txt" @echo Writing text to draft.txt> Desktop:\testing\draft.txt

This means that when I execute the batch file, I want a draft.txt file with some text in it to be created in the testing folder that I have created in my desktop. I want the batch file to be executed when I run my Java class. However, I get the following error:

There is no program associated to perform the requested action. Please install a program or, if one is already installed, create an association in the Default Programs control panel.

Here is my Java class:

public class DraftBatchFile {
   public DraftBatchFile() {
    super();
   }

  /**Main Method
  * @param args
  */
  public static void main(String[] args) {
    //Get Runtime object
    Runtime runtime = Runtime.getRuntime();
    try {
        //Pass string in this format to open Batch file
        runtime.exec("cmd /c start Desktop:\\DraftBatchFile.bat");
    } catch (IOException e) {
        System.out.println(e);
    }
  }
}

How do I get the batch file to execute the commands when I run the Java class? I am even unable to run the Java class. Why is that so? Do I have to add more code? Someone, please help me as I am new to this. Thank you so much.

Upvotes: 1

Views: 84

Answers (1)

ACatInLove
ACatInLove

Reputation: 540

Desktop: means nothing.

    "%userprofile%\Desktop\Testing\Draft.txt"

Will do what you mean. (Note the quotes)

     Driveletter:\Folder\File.ext

So

     c:\windows\win.ini

See Command to run a .bat file for additional info.

Upvotes: 2

Related Questions