4

Where did Java and Windows end? File separator question

 2 years ago
source link: https://www.codesd.com/item/where-did-java-and-windows-end-file-separator-question.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Where did Java and Windows end? File separator question

advertisements

I want to launch a Java jar by Runtime but Windows is making problems again. I know this must have come up hundreds of times but I tried a few things (like String.replace("/","\")) and since I cannot debug on Windows this is taking quiet some time. This works fine under Unix:

public boolean run(String args[], String workingDir, boolean output) throws 

    FileNotFoundException {
                if (args.length <= 0) {
                    System.err.println("No cmd provided");
                }
                if (workingDir == null) {
                    workingDir = "./";
                }
                ProcessBuilder pb = new ProcessBuilder(args);

                pb.directory(new File(workingDir));
                Process p;
                int exitValue = 0;
                try {
                    p = pb.start();
                    InputStream is = p.getInputStream();
                    InputStreamReader isr = new InputStreamReader(is);
                    BufferedReader br = new BufferedReader(isr);

                    String line;
                    p.waitFor();
                    if (output) {
                        while ((line = br.readLine()) != null) {
                            setChanged();
                            notifyObservers(line);
                        }
                    }
                    exitValue = p.exitValue();
                } catch (InterruptedException ex) {
                    return false;
                } catch (IOException ex) {
                    return false;
                }
                if (exitValue == 0) {
                    return true;
                } else {
                    throw new FileNotFoundException();
                }
            }

launched by:

public static void launchApp(String subPath) throws FileNotFoundException {
    String[] args = String.format("java -jar -Xdock:name=AppName -Xdock:icon=%sicon.icns %AppName.jar", subPath, subPath).split(" ");
    ExecRuntime.run(args, null);
}


Use the standard java system properties when doing platform specific things in Java to ensure that your application is portable. See System Properties for more information on some of the values available to your app.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK