【问题标题】:Window Batch file integrate into Java GUI窗口批处理文件集成到 Java GUI
【发布时间】:2014-07-31 09:05:42
【问题描述】:

您好,我想将我的批处理文件 (stages.bat) 集成到我的 Java Gui 中,但我不知道如何操作。我参考了这两个网站:

Integrating batch script into Java GUI?How to integrate batch script multiple selections into JAVA GUI?

但是,这些答案并没有多大帮助,并且在我运行我的 java 代码时会出现错误。

这是我的批处理代码(示例):

@echo off
setLocal Enabledelayedexpansion

for %%d in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
if exist %%d:\ (
    if exist %%d:\stages.bat (
        %%d:
        )
    )
)

@For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @( 
   Set Day=%%A
   Set Month=%%B
   Set Year=%%C
)

setlocal enableDelayedExpansion
set "baseName=Stage-%Year%%Month%%Day%-"
set "n=0"
:loop
set /a n+=1
if exist "%baseName%%n%.txt" goto :loop
rem type nul > "%baseName%%n%.txt"

SETLOCAL ENABLEDELAYEDEXPANSION 
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET sourcedir=%d%
SET "choicebet= 123456789abcdefghijklmnopqrstuvwxyz"
FOR /f "tokens=1*delims=[]" %%a IN (
  'dir /b /a-d "%sourcedir%\*.raw" ^|find /n /v ""'
  ) DO (
 SET $!choicebet:~%%a,1!=%%b
 SET /a choices=%%a+1
)
)
CALL SET choices=%%choicebet:~0,!choices!%%
IF DEFINED $2 (
 cls
 FOR /f "tokens=1*delims==" %%a IN ('set $') DO SET "choicemade=%%a : %%b"&ECHO(!choicemade:~1!
 choice /c %choices:~1% /M "You have MORE than 1 raw files. Please select one."
 REM SET /a choicemade=!errorlevel!-1
 CALL SET choicemade=$%%choicebet:~!errorlevel!,1%%
 ) ELSE (
 SET choicemade=$1
)
FOR /f "tokens=1*delims==" %%a IN ('set %choicemade%') DO SET choicemade=%%b

cls

setlocal enabledelayedexpansion
echo Please select your functions for %sourcedir%%choicemade%
echo.
echo 1. Stage1
echo 2. Stage2
echo 3. Stage3
echo 4. EndStage
echo 5. All
echo.

:getOptions
set /p "op=Type the number of the functions you need for analysis without spacing (e.g. 1,2,3): "

if not defined op ( 
   echo.
    echo Please enter a valid option
    goto getOptions
   )

cls

SET StartTime=%time%


for %%a in (%op%) do if %%a EQU 5 set op=1,2,3,4
for %%i in (%op%) do call :option-%%i 

echo.
echo Analysis Done
echo Start Time (HH:MM:SS.MS): %StartTime%
echo End Time (HH:MM:SS.MS):%time%
echo.

choice /c yn /m "Would you like to continue with the optional functions?"
if %errorlevel% equ 1 (
    SET StartTime2=%time%
    echo Time to start
    echo *** choose ***
    echo Start Time (HH:MM:SS.MS): %StartTime2%
    echo End Time (HH:MM:SS.MS):%time%
    pause
    exit
    )
 if %errorlevel% equ 2 (
    exit
   )

:option-1
echo Action made.
echo Better analysis to be done later.
echo Start %choicemade% soon >> %baseName%%n%.txt

exit /B

:option-2
echo Stage 2 done.
echo Recording %choicemade% soon >> %baseName%%n%.txt

exit /B

:option-3
echo harry potter maybe included in it
echo Ending %choicemade% soon >> %baseName%%n%.txt

exit /B

:option-4
 echo Bye bye
Echo End %choicemade% >> %baseName%%n%.txt
exit /B

这是我的 java 代码(示例):

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;

import java.awt.Window.Type;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.JButton;

 public class GUI extends JFrame {
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GUI frame = new GUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static String cmdLine = "";
    private static final String scriptFile = "stages.bat";

    public GUI() {
        setTitle("Raw Files");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);

        JCheckBox chckbxStage = new JCheckBox("STAGE1");
        chckbxStage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(!cmdLine.contains("STAGE1"))
                     cmdLine += " STAGE1 ";
            }
        });
        chckbxStage.setBounds(45, 36, 97, 23);
        contentPane.add(chckbxStage);

        JCheckBox chckbxStage_1 = new JCheckBox("STAGE2");
        chckbxStage_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(!cmdLine.contains("STAGE2"))
                     cmdLine += " STAGE2 ";
            }
        });
        chckbxStage_1.setBounds(45, 89, 97, 23);
        contentPane.add(chckbxStage_1);

        JCheckBox chckbxStage_2 = new JCheckBox("STAGE3");
        chckbxStage_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(!cmdLine.contains("STAGE3"))
                     cmdLine += " STAGE3 ";
            }
        });
        chckbxStage_2.setBounds(253, 36, 97, 23);
        contentPane.add(chckbxStage_2);

        JCheckBox chckbxEndstage = new JCheckBox("ENDSTAGE");
        chckbxEndstage.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(!cmdLine.contains("ENDSTAGE"))
                     cmdLine += " ENDSTAGE ";
            }
        });
        chckbxEndstage.setBounds(253, 89, 97, 23);
        contentPane.add(chckbxEndstage);

        JCheckBox chckbxA = new JCheckBox("ALL");
        chckbxA.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                 cmdLine = "STAGE1 STAGE2 STAGE3 ENDSTAGE";
            }
        });
        chckbxA.setBounds(45, 142, 97, 23);
        contentPane.add(chckbxA);
        JButton btnOkay = new JButton("Okay");
        btnOkay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                DetectDrive d = new DetectDrive();
                String DetectDrive = d.USBDetect();

                try {

                    String command = "cmd /c start " + DetectDrive+ "stages.bat";
                    Runtime.getRuntime().exec(scriptFile + cmdLine);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        btnOkay.setBounds(323, 208, 89, 23);
        contentPane.add(btnOkay);

    }
}

我还希望我的批处理脚本在我的 gui 中作为子进程或后台运行。任何人都可以帮助我,因为我还是 Java 新手吗?谢谢

编辑:

我不明白错误,因为它们太多了:

java.io.IOException: Cannot run program "stage.bat": CreateProcess error=2, 系统找不到指定的文件 在 java.lang.ProcessBuilder.start(未知来源) 在 java.lang.Runtime.exec(未知来源) 在 java.lang.Runtime.exec(未知来源) 在 java.lang.Runtime.exec(未知来源) 在 GUI$7.actionPerformed(GUI.java:110) 在 javax.swing.AbstractButton.fireActionPerformed(未知来源) 在 javax.swing.AbstractButton$Handler.actionPerformed(未知来源) 在 javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在 javax.swing.DefaultButtonModel.setPressed(未知来源) 在 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) 在 java.awt.Component.processMouseEvent(未知来源) 在 javax.swing.JComponent.processMouseEvent(未知来源) 在 java.awt.Component.processEvent(未知来源) 在 java.awt.Container.processEvent(未知来源) 在 java.awt.Component.dispatchEventImpl(未知来源) 在 java.awt.Container.dispatchEventImpl(未知来源) 在 java.awt.Component.dispatchEvent(未知来源) 在 java.awt.LightweightDispatcher.retargetMouseEvent(未知来源) 在 java.awt.LightweightDispatcher.processMouseEvent(未知来源) 在 java.awt.LightweightDispatcher.dispatchEvent(未知来源) 在 java.awt.Container.dispatchEventImpl(未知来源) 在 java.awt.Window.dispatchEventImpl(未知来源) 在 java.awt.Component.dispatchEvent(未知来源) 在 java.awt.EventQueue.dispatchEventImpl(未知来源) 在 java.awt.EventQueue.access$400(未知来源) 在 java.awt.EventQueue$3.run(未知来源) 在 java.awt.EventQueue$3.run(未知来源) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源) 在 java.awt.EventQueue$4.run(未知来源) 在 java.awt.EventQueue$4.run(未知来源) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源) 在 java.awt.EventQueue.dispatchEvent(未知来源) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) 在 java.awt.EventDispatchThread.pumpEventsForFilter(未知来源) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源) 在 java.awt.EventDispatchThread.pumpEvents(未知来源) 在 java.awt.EventDispatchThread.pumpEvents(未知来源) 在 java.awt.EventDispatchThread.run(未知来源) 原因:java.io.IOException: CreateProcess error=2, 系统找不到指定的文件 在 java.lang.ProcessImpl.create(本机方法) 在 java.lang.ProcessImpl.(未知来源) 在 java.lang.ProcessImpl.start(未知来源) ... 41 更多

谁能帮我理解这些错误的含义?我非常感谢我能得到的任何帮助。谢谢。

【问题讨论】:

  • 您的代码有什么问题?你有任何错误吗?
  • @ThusithaThilinaDayaratne 我已经用我面临的错误更新了我的问题。请看一下,感谢您的帮助。
  • "系统找不到指定的文件" 检查文件位置是否正确

标签: java windows batch-file user-interface


【解决方案1】:

我发现您的代码中有 2 个错误。

  1. 您设置了“命令”,但您使用“脚本文件 + cmdLine”执行。

    String command = "cmd /c start" + DetectDrive+ "stages.bat"; Runtime.getRuntime().exec(scriptFile + cmdLine);

  2. 要调用批处理文件,您必须按以下方式进行。如果批处理文件不在当前目录中,则必须提供批处理文件的路径

    字符串命令 = "cmd /c start" + "c:\temp\hello.bat"; Runtime.getRuntime().exec(command);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 2012-09-12
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多