2

I have created a java class to execute a batch file that is in my desktop so that the commands in the batch file will be executed too. The problem is that, i keep getting the error:

The filename, directory name, or volume label syntax is incorrect. The filename, directory name, or volume label syntax is incorrect.

I have checked the .bat name and the directory. It is correct. When i type cmd /c start C:/Users/attsuap1/Desktop, windows explorer opens the desktop tab. However when i type cmd /c start C:/Users/attsuap1/Desktop/DraftBatchFile.bat, it gives the error. My DraftBatchFile.bat is in my desktop.

Here are my java codes:

public class OpenDraftBatchFile{
public OpenDraftBatchFile() {
    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 C:/Users/attsuap1/Desktop/DraftBatchFile.bat");
    } catch (IOException e) {
        System.out.println(e);
    }
}

Why is it that the batch file cannot be executed even if the directory is correct? Someone please help me. Thank you so much.

These are the codes in DraftBatchFile.bat

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

When i execute the DraftBatchFile.bat by running the java class, i want a draft.txt file to be created in a testing folder that i have created (in desktop).

10
  • Check the PATH environment variable. See the link to change the path java.com/en/download/help/path.xml Commented Nov 23, 2017 at 8:29
  • Hi, thanks for helping. What should be added to the Path environment? Commented Nov 23, 2017 at 8:34
  • Java PATH directory, C:\Program Files\Java\jdk1.7.0_67\bin Commented Nov 23, 2017 at 8:34
  • I have already set that Commented Nov 23, 2017 at 8:37
  • 1
    @YevhenDanchenko it is better to use %userprofile%\Desktop as per my answer below because if this is being run on other systems, user attsuap will not exist Commented Nov 23, 2017 at 9:01

2 Answers 2

2

there is no such thing as desktop:\

Instead try something like this.

@echo off
echo . %userprofile%\Desktop\testing\dblank.txt
@echo Writing text to draft.txt > %userprofile%\Desktop\testing\dblank.txt
Sign up to request clarification or add additional context in comments.

6 Comments

I still get the error The filename, directory name, or volume label syntax is incorrect. i got no idea why :( i edited the batch file. I edited runtime.exec("cmd /c start %userprofile%/Desktop/DraftBatchFile.bat"); that line of code in my java class however when i run my java class i still dont get what i want it still gives error
I want the draft.txt file to be created in the testing folder i have created in my desktop. Actually, i just have to prove, when i run my java class, my batch file is being executed. Is there any other simpler commands that i can put in my batch file to prove that?
I will create a new batch file instead and try it out
@SushaNaidu try my batch code now please, I edited my answer I forgot to replace the desktop:\ part in the first echo.
IT WORKED ! Thank you so much ! :)
|
0

You just need to change a directory and create new subdirectory if it not exist. My offer is change .bat file like this:

@echo off
if not exist "%userprofile%\Desktop\testing" mkdir "%userprofile%\Desktop\testing"
echo.>"%userprofile%\Desktop\draft.txt"
@echo Writing text to draft.txt>"%userprofile%\Desktop\draft.txt"

Also you can create .txt file and write in it text through Java code:

    File directory = new File(System.getProperty("user.home")+"//Desktop//testing");
    if (!directory.exists())
         directory.mkdirs();
    String content = "Writing text to draft.txt";
    File file = new File(System.getProperty("user.home")+"//Desktop//testing//draft.txt");
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(content);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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.