Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7971243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:35:28+00:00 2026-06-04T07:35:28+00:00

I have to open a .exe file from my Java program. So I tried

  • 0

I have to open a .exe file from my Java program. So I tried following code First.

Process process = runtime.exec("c:\\program files\\test\\test.exe");

But I was getting some error. Then I found out that the exe has to be launched from that location that is c://program files/test/ only then it will open with out errors. So I decided to write a .bat file and execute so that it will cd to that location and execute the .exe file.

Following is my code:

BufferedWriter fileOut;

String itsFileLocation = "c:\\program files\\test\\"
    System.out.println(itsFileLocation);
    try {
     fileOut = new BufferedWriter(new FileWriter("C:\\test.bat"));
     fileOut.write("cd\\"+"\n");
     fileOut.write("cd "+ itsFileLocation +"\n");
     fileOut.write("test.exe"+"\n");
     fileOut.write("exit"+"\n");
     
     fileOut.close(); // Close the output stream after all output is done.
    } catch (IOException e1) {
     e1.printStackTrace();
    } // Create the Buffered Writer object to write to a file called filename.txt
    Runtime runtime = Runtime.getRuntime();
    try {
     Process process =runtime.exec("cmd /c start C:\\test.bat");
    } catch (IOException e) {
     e.printStackTrace();
    }

The above code works perfectly. However, the command prompt is also opened at the back of my .exe (Application). It closes only after the .exe file exits..

I need to clse my command prompt when my application stats.

My .bat file will be like following after it is written by the program.

cd\
cd C:\Program Files\test\
test.exe
exit
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:35:29+00:00Added an answer on June 4, 2026 at 7:35 am

    You don’t need a console. You can execute a process using a working directory:

    exec(String command, String[] envp, File dir)

    Executes the specified string command in a separate process
    with the specified environment and working directory.

    • command is the location of the .exe
    • envp can be null
    • dir, is the directory of your .exe

    With respect to your code it should be…

    Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have help.html file in resource directory. I can open it from Eclipse these
I have the following code which is executed from the command line: import cgi,time,os,json,sys,zipfile,urllib2
I have made a script to open Spotify with wine: #!/bin/bash DIR=/home/jorgsk/.wine/drive_c/Program Files/Spotify/ cd
I have the following code in a batch file that is called by a
I have to open a page from class extended from javax.swing.AbstractAction class... Is that
I have a open file dialog with three filters: QString fileName = QFileDialog::getOpenFileName( this,
I am trying to run a .exe file from Javascript. This is what I
I have a windows service which is trying to access an xml file from
I have an exe file that I have to call with several parameters, and
How can I hide my table database file from main application. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.