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 8381813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:46:50+00:00 2026-06-09T16:46:50+00:00

I made 2 .java files and 1 .bat file. Lets refer to the first

  • 0

I made 2 .java files and 1 .bat file. Lets refer to the first .java file as “First.java” and the second as “Second.java”. Batch file is “compile.bat”. Lets say First.java is located in “…/Desktop/temp/First.java”, and Second.java is in “…/Desktop/Test/Second.java” along with a batch file (“…/Desktop/Test/compile.bat”). Now, compile.bat consists of this code:

cmd /c "cd C:\blahblah\temp && javac First.java"

I tested it out both in CMD and by double clicking, it works. I want to compile First.java from the Second.java with this code:

Process p1 = Runtime.getRuntime().exec("C:\\blahblah\\compile.bat");

(I have to navigate to dir since the default dir of CMD is not the same as the directory of compile.bat). This doesn’t work. It seems it simply ignores the code. As I said, I tried a lot of different things, I even tried using some other library that should have changed the dir. Help me please!

  • 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-09T16:46:52+00:00Added an answer on June 9, 2026 at 4:46 pm

    Easiest way: you should use start. Setting working dir is also possible, look at this example:

    Runtime r=Runtime.getRuntime();
        r.exec("cmd.exe /c start compile.bat",         //path to executable
                null,                                  // env vars, null means pass parent env
                new File("C:\\blahblah"));              // working directory 
    

    Additional information:
    If you do not want to start your process in it’s separate console (that’s what start does) you can do r.exec("cmd.exe /c compile.bat");, but because it executes in context of it’s parent console, you must wait with p.waitFor() or read it’s input stream – otherwise it may silently fail.
    This will run and display output from you command:

    Process p=r.exec("cmd.exe /c compile.bat",         //path to executable
                null,                                  // env vars, null means pass parent env
                new File("C:\\blahblah"));
    
        InputStream is=p.getInputStream();
        BufferedReader br= new BufferedReader(new InputStreamReader(is));
    
        String line=new String();
        while ((line=br.readLine())!=null) System.out.println (line);
    

    Also as of 1.5 ProcessBuilder is preffered way to start processes (from Java doc):

    java.lang.Process

    The methods that create processes may not work well for special
    processes on certain native platforms, such as native windowing
    processes, daemon processes, Win16/DOS processes on Microsoft Windows,
    or shell scripts.

    By default, the created subprocess does not have its own terminal or
    console. All its standard I/O (i.e. stdin, stdout, stderr) operations
    will be redirected to the parent process, where they can be accessed
    via the streams obtained using the methods getOutputStream(),
    getInputStream(), and getErrorStream(). The parent process uses these
    streams to feed input to and get output from the subprocess. Because
    some native platforms only provide limited buffer size for standard
    input and output streams, failure to promptly write the input stream
    or read the output stream of the subprocess may cause the subprocess
    to block, or even deadlock.

    Where desired, subprocess I/O can also be redirected using methods of
    the ProcessBuilder class.

    The subprocess is not killed when there are no more references to the
    Process object, but rather the subprocess continues executing
    asynchronously.

    There is no requirement that a process represented by a Process object
    execute asynchronously or concurrently with respect to the Java
    process that owns the Process object.

    As of 1.5, ProcessBuilder.start() is the preferred way to create a
    Process.

    Since: JDK1.0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a java application that stores data from a .csv file to
I'm trying to use sbt to compile an Android Project made in Java, which
I had a different problem first, so I made this post: Java JPanel mouse
I have made a java application and wants to generate log files so whenever
Last summer, I made a Java application that would parse some PDF files and
We have normal java files residing in the SVN. We have made some changes
I made a really simple java example with two files: Dog.java contains: package com.greg.dog;
so i have made some java files which provide some specific API functionality. I
I made a java application.and bundled all classes in jar file..wen i run the
I usually write java code using a simple text editor and .bat files to

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.