1

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.

Susha Naidu
  • 307
  • 1
  • 3
  • 16
  • Why do you use `Desktop:` instead of the absolute path the the file? What do you mean by `I am even unable to run the Java class` ? What is the issue? – saw303 Nov 23 '17 at 06:44
  • The file is located at desktop. When i run the java class i get the error stating `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.` i got no idea why is that so – Susha Naidu Nov 23 '17 at 06:46
  • The problem is that your Windows does not know what to do with the `Desktop:` protocol since no application has been assigned as the protocol handler. But what happens when you run `cmd /c start %USERPROFILE%\Desktop\DraftBatchFile.bat`? – saw303 Nov 23 '17 at 06:52
  • I have to run the java class to execute the DraftBatchFile.bat and when i run the java class, the draft.txt must be created in the testing folder. How do i go about doing that please do help me – Susha Naidu Nov 23 '17 at 06:52
  • @saw303 When i run `cmd /c start %USERPROFILE%\Desktop\DraftBatchFile.bat`, a command prompt opens and displays `The filename, directory name, or volume label syntax is incorrect. The filename, directory name, or volume label syntax is incorrect.` – Susha Naidu Nov 23 '17 at 06:59
  • I have double checked, the filename is DraftBatchFile.bat and it is in desktop – Susha Naidu Nov 23 '17 at 07:00
  • You should check the path of the file. In Windows Explorer right-click on the file and look for the Details-Pane. – Ralf Renz Nov 23 '17 at 07:06
  • Does it matter if i have two slashes? `cmd /c start %USERPROFILE%\\Desktop\\DraftBatchFile.bat` With one slash, i get the red line under the directory part (error) – Susha Naidu Nov 23 '17 at 07:18
  • @RalfRenz Hi, i checked and typed the exact same thing stated in the Details pane, `cmd /c start C:\\Users\\susha\\Desktop\\DraftBatchFile.bat` i still get `The filename, directory name, or volume label syntax is incorrect. The filename, directory name, or volume label syntax is incorrect.` error :( – Susha Naidu Nov 23 '17 at 07:23
  • OK. Maybe the error comes from inside your bat-file. Test this by filling the bat-file with a simple "echo hallo". - Does this work? – Ralf Renz Nov 23 '17 at 08:44
  • Sorry i am new to this. Do i have to just open cmd and type "echo hallo"? If yes, i get hallo – Susha Naidu Nov 23 '17 at 08:45
  • The three lines at the beginning of the thread are the content of the DraftBatchFile? Then you have to correct the paths in that file too. – Ralf Renz Nov 23 '17 at 08:54

1 Answers1

2

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.

ACatInLove
  • 530
  • 2
  • 5