繁体   English   中英

尝试使用 Java Swing 和 Filechooser 但我的代码中有错误

[英]Trying to use Java Swing and Filechooser but there is an error in my code

我有一个带有 GUI 的 java 程序,如下所示。 它的作用是由一个按钮组成,该按钮允许用户选择文件,并在选择文件时打开命令提示符并切换到该文件的目录。

但是,此代码不会打开命令提示符。 如何修改代码以便更改到所选文件的目录?

public class GUIProject {

    public static void main(String[] args) {
        JFrame f=new JFrame();
        //final JTextField tf=new JTextField();
        JTextArea File1= new JTextArea("Select the program to test.");
        JTextArea File2= new JTextArea();
        File1.setBounds(50,50, 150,20);
        JButton b=new JButton("Select");
        b.setBounds(50,100,95,30);
        b.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                try {
                    File1();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        f.add(b);
        f.add(File1);
        f.add(File2);
        f.setSize(400,400);
        f.setLayout(null);
        f.setVisible(true);
    }

    public static void File1() throws IOException{
        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        int returnValue = jfc.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();

            ProcessBuilder builder = new ProcessBuilder(
                    "cmd.exe", "/c", "cd \"selectedFile.getAbsolutePath()\" && dir");
            builder.redirectErrorStream(true);
            Process p = builder.start();
        }
    }
}

更新 -

    public static void File2() throws IOException{
            JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());

            int returnValue = jfc.showOpenDialog(null);
            

            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = jfc.getSelectedFile();
                
        
                System.out.println(selectedFile.getAbsolutePath()); 
                String[] commands = {"cd selectedFile.getAbsolutePath()", "mvn clean test -DskipTests -Dmaven.compiler.source=1.8 -Dmaven.compiler.target=1.8 -B -f C:\\Users\\A\\Documents\\GitHub\\ThisNew\\flacoco\\examples\\exampleFL1\\FLtest1", "java -jar target/flacoco-1.0.6-SNAPSHOT-jar-with-dependencies.jar --projectpath examples\\exampleFL1\\FLtest1 --output finalresult.csv --format CSV"};
                String command = "cmd.exe /c " + String.join(" && ", commands);
                ProcessBuilder pb = new ProcessBuilder(command.split(" "));
                pb.inheritIO(); //to see the result in the console
                pb.start();
            
    
        
              }
        
        }

使用我在下面编写的这段代码,您可以打开 cmd 并将其设置在所需的目录中。

import java.io.File;
import java.io.IOException;

public class main {
    public static void main( String[] args ) {
        try{
            String command = "ping www.google.com";
            //remove "\"start;" to hide cmd
            String[] cmd       = new String[]{ "cmd.exe", "/C", "\"start;" + command + "\"" };
            File     directory = new File( "C:/Users" );

            ProcessBuilder pb = new ProcessBuilder( cmd );
            pb.directory( directory );

            Process process = pb.start();

        }catch( IOException e ){
            System.out.println( "Something has gone wrong" );
            e.printStackTrace();
        }
    }
}

让我知道它是否有帮助。

如果要运行多个命令,可以将它们与&&放在一起,如下所示:

cd "C:/users/" && echo hello world && echo I will be executed after

其输出将是:

hello world
I will be executed after

C:\Users>

在 Java 中,这可能看起来像:

String[] commands = {"cd C:/users/", "echo Hello world", "echo I will be executed after"};
String command = "cmd.exe /c " + String.join(" && ", commands);
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.inheritIO(); //to see the result in the console
pb.start();

流程构建器不应打开新窗口。 默认情况下,它在后台执行此操作。 您可以使用参数/c start来欺骗它打开一个 cmd 窗口。 添加另一个参数来设置新窗口的位置: cmd /c start /D "C:/users/"

使用流程构建器可以如下所示:

String command = "cmd /c start /D \"C:/users/\"";
ProcessBuilder pb = new ProcessBuilder(command.split(" "));
pb.start();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM