您好我想将我的批处理文件(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:无法运行程序" 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(未知来源) at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source) 在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源) 在javax.swing.DefaultButtonModel.setPressed(未知来源) 在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源) at java.awt.Component.processMouseEvent(Unknown Source) 在javax.swing.JComponent.processMouseEvent(未知来源) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access $ 400(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) 在java.awt.EventQueue $ 3.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) 在java.awt.EventQueue $ 4.run(未知来源) 在java.awt.EventQueue $ 4.run(未知来源) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 引起:java.io.IOException:CreateProcess error = 2,系统找不到指定的文件 at java.lang.ProcessImpl.create(Native Method) 在java.lang.ProcessImpl。(未知来源) at java.lang.ProcessImpl.start(Unknown Source) ......还有41个
任何人都可以帮我理解这些错误是什么意思?我真的很感激我能得到的任何帮助。谢谢。
答案 0 :(得分:0)
我在你的代码中看到了2个错误。
您设置了“命令”,但您正在使用“scriptfile + cmdLine”来执行。
String command =“cmd / c start”+ DetectDrive +“stages.bat”; Runtime.getRuntime()。exec(scriptFile + cmdLine);
要调用批处理文件,您必须按以下方式执行此操作。如果批处理文件不在当前目录中,则必须提供批处理文件的路径
String command =“cmd / c start”+“c:\ temp \ hello.bat”; 调用Runtime.getRuntime()EXEC(命令);