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

  • Home
  • SEARCH
  • 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 8510275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:39:27+00:00 2026-06-11T03:39:27+00:00

I can run a bat file in java, I can also get it to

  • 0

I can run a bat file in java, I can also get it to send info to java.

Code to run the bat file below. I think i can use an input stream to somehow get progress. I was also thinking maybe I could leave things in the bat file for it to read to get status.

I am hoping someone can give me some code on how to add a basic progress bar to this. I read the demo and examples from oracle but they don’t cover get progress from a bat file.

I updated my code below, I still need to run it and get the line count for the true 100% completion. However, it’s not showing the progress like I thought it would. It runs and stays at 0%, I can tell it’s running still because of task manager.

public class ProgressBar extends JPanel
                          implements ActionListener, 
                                     PropertyChangeListener {

private JProgressBar progressBar;
private JButton startButton;
private JTextArea taskOutput;
private Task task;

class Task extends SwingWorker<Void, Void> {
    /*
     * Main task. Executed in background thread.
     */
    @Override
    public Void doInBackground() {
        Random random = new Random();
        int progress = 0;
        //Initialize progress property.
        setProgress(0);
        //Sleep for at least one second to simulate "startup".
        try {
            Thread.sleep(1000 + random.nextInt(2000));
        } catch (InterruptedException ignore) {}
       // while (progress < 100) {
            //Sleep for up to one second.
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException ignore) {}
            //Make random progress.
            try { 
                String ls_str;   
                Process ls_proc = Runtime.getRuntime().exec("\\WMIRebuild.bat"); 
            //     get its output (your input) stream    
                DataInputStream ls_in = new DataInputStream( 
                    ls_proc.getInputStream()); 
                try { 
                while ((ls_str = ls_in.readLine()) != null) { 
                    System.out.println(ls_str);
                    progress++;
                    } 
                } catch (IOException e) {
                System.exit(0); 
                } 
            } catch (IOException e1) { 
                System.err.println(e1); 
                System.exit(1); 
            }
            setProgress(Math.min(progress, 100));
       // }
        return null;
    }

    /*
     * Executed in event dispatch thread
     */
    public void done() {
        Toolkit.getDefaultToolkit().beep();
        startButton.setEnabled(true);
        taskOutput.append("Done!\n");
    }
}

public ProgressBar() {
    super(new BorderLayout());

    //Create the demo's UI.
    startButton = new JButton("Start");
    startButton.setActionCommand("start");
    startButton.addActionListener(this);

    progressBar = new JProgressBar(0, 18);
    progressBar.setValue(0);

    //Call setStringPainted now so that the progress bar height
    //stays the same whether or not the string is shown.
    progressBar.setStringPainted(true); 

    taskOutput = new JTextArea(5, 20);
    taskOutput.setMargin(new Insets(5,5,5,5));
    taskOutput.setEditable(false);

    JPanel panel = new JPanel();
    panel.add(startButton);
    panel.add(progressBar);

    add(panel, BorderLayout.PAGE_START);
    add(new JScrollPane(taskOutput), BorderLayout.CENTER);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

/**
 * Invoked when the user presses the start button.
 */
public void actionPerformed(ActionEvent evt) {
    progressBar.setIndeterminate(true);
    startButton.setEnabled(false);
    //Instances of javax.swing.SwingWorker are not reusuable, so
    //we create new instances as needed.
    task = new Task();
    task.addPropertyChangeListener(this);
    task.execute();
}

/**
 * Invoked when task's progress property changes.
 */
public void propertyChange(PropertyChangeEvent evt) {
    if ("progress" == evt.getPropertyName()) {
        int progress = (Integer) evt.getNewValue();
        progressBar.setIndeterminate(false);
        progressBar.setValue(progress);
        taskOutput.append(String.format(
                    "Completed %d%% of task.\n", progress));
    }
}

/**
 * Create the GUI and show it. As with all GUI code, this must run
 * on the event-dispatching thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("ProgressBarDemo2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new ProgressBar();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

}

  • 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-11T03:39:28+00:00Added an answer on June 11, 2026 at 3:39 am

    Have the progress bar’s frame separate from your main() method. Have it run on a thread of its own. As you read stuff from the batch file ls_in.readLine(), update the progress in the other frame. For example, you can have a method in your frame called addTick() which adds another block to the progress bar, that can be called each time you read a line from your batch file.


    very rudimentary example:

    Thread that reads from process:

        while ((ls_str = ls_in.readLine()) != null) { 
            System.out.println(ls_str);
            progressBar.addTick();
        } 
    

    Progress Bar, runs on its own thread:

        String bar = "#";
    
        public void addTick()
        {
            if(bar.length() < maxBarLength) {
                bar = bar + "#";
                drawBar(bar);
            }
        }
    
        public void draw()
        {
            drawBar(bar);
        }
    
        public void paint()
        {
            // draw some stuff
            draw(bar);
        }
    

    I don’t know, something like that. Obviously the progress bar is going to be different from this, this is just pseudo-code to give you an idea of how the main() method interacts with the progress bar.

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

Sidebar

Related Questions

I need to run a bat file using a java code. I did that
I have an java application which contains run.bat file as below: rem set path=D:/Applns/jdk1.5/bin
I'd like to write a .bat file that can run same .exe file multiple
I have a java program that is supposed to run a bat file, and
I'm trying to run a .bat file from my Java app. I've tried all
I try to run JSBuilder script (*.bat file): java.exe -jar JSBuilder2.jar --projectFile script_name.jsb2 --homeDir
How can I run a CMD or .bat file in silent mode? I'm looking
I can run the following query in PHPMyAdmin, but for some reason I get
I can run my program outside of a jar file, but I want to
to run a single file you can run in mysql .\ filename or you

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.