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.

12
  • 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? Commented Nov 23, 2017 at 6: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 Commented Nov 23, 2017 at 6: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? Commented Nov 23, 2017 at 6: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 Commented Nov 23, 2017 at 6: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. Commented Nov 23, 2017 at 6:59

1 Answer 1

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.