简体   繁体   中英

Java codes to run program from detected drive

I have a program that I want to run from a detected USB drive (removable storage such as a USB), and this was done by creating two classes: external.java and DetectDrive.java as follows:

external.java

 public class external
 {
    public static void main(String args[]) throws InterruptedException  
    {

    DetectDrive d = new DetectDrive();
    String DetectDrive = d.USBDetect();
    BufferedWriter fileOut;
    String filePath = DetectDrive;
    System.out.println(filePath);

    try 
        {
        fileOut = new BufferedWriter(new FileWriter("F:\\external.bat"));
        fileOut.write("cd "+ filePath +"\n");
        fileOut.write("external.exe"+"\n");

        fileOut.close(); //close the output stream after all output is done
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec("cmd /c start" +DetectDrive+ "\\external.bat");
        p.waitFor();
        } catch (IOException e){
            e.printStackTrace();
            }       
    }
}

DetectDrive.java

import java.io.*;
import java.util.*;
import javax.swing.filechooser.FileSystemView;

public class DetectDrive
{
    public String USBDetect()
    {
        String driveLetter = "";
        FileSystemView fsv = FileSystemView.getFileSystemView();

        File[] f = File.listRoots();
        for (int i = 0; i < f.length; i++)
        {
            String drive = f[i].getPath();
            String displayName = fsv.getSystemDisplayName(f[i]);
            String type = fsv.getSystemTypeDescription(f[i]);
            boolean isDrive = fsv.isDrive(f[i]);
            boolean isFloppy = fsv.isFloppyDrive(f[i]);
            boolean canRead = f[i].canRead();
            boolean canWrite = f[i].canWrite();

            if (canRead && canWrite && !isFloppy && isDrive && (type.toLowerCase().contains("removable") || type.toLowerCase().contains("rimovibile")))
            {
                //log.info("Detected PEN Drive: " + drive + " - "+ displayName); 
                driveLetter = drive;
                break;
            }
        }

        /*if (driveLetter.equals(""))
        {
            System.out.println("Not found!");
        } 
        else 
        {
            System.out.println(driveLetter);
        }
        */

        //System.out.println(driveLetter);
        return driveLetter;
    }
}

The problem now is that there are no errors when I run the external.java (main). However, the output only shows the detected drive which is F:\\ but it doesn't run the specified program which is external.exe and it also mentioned that the program got terminated. Can someone please help me point out where I went wrong and what the correct codes should be like? I am new to Java.

I got the DetectDrive codes from http://www.snip2code.com/Snippet/506/Detect-USB-removable-drive-in-Java which I believe is now in maintenance.

Is it possible to change the new FileWriter(" F:\\external.bat ") to detect the USB drive directory instead? For example letting the program detect the usb drive and automatically put in the correct directory instead of us typing the F:\\ manually. I have no answer for this yet. Please help!

It seems problem is with command :

"cmd /c start" +DetectDrive+ "\\external.bat"

It should have a {space} after start:

"cmd /c start " +DetectDrive+ "\\external.bat"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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