简体   繁体   中英

how to open a file in a .jar through java

I want to open a file in .jar application and I want to use java to do this. Explaining, for example I have the file SF_Antivalent.xml and I want to open it with uppaal.jar. How do I do this using Java. I've written the following code, but it doesn't work.

public class test7 {
public static void main(String[] args){


    Runtime rt=Runtime.getRuntime();
    String file="C:\\Users\\V\\Documents\\diplwmatiki\\SFBs\\SF_Antivalent.xml";
    Process p=rt("C:\\Windows\\System32\\java.exe", "-jar", "C:\\Users\\"
            + "V\\Documents\\uppaal-4.0.13-aca\\uppaal-4.0.13\\uppaal.jar" + file);
}

}

and I get this error: the method rt(String, String, String) is undefined for the type test. Is there something to do?

The reason you are getting the error is because rt is a Runtime object, not a method. To call a method of rt do this:

rt.someMethodName();

With the code above you cannot get XML file, you're trying to execute something, instead of opening file inside JAR archive

Look into getResourceAsStream , this will give you possibility to load any file from JAR

You question is a little confusing, however, I believe you want to run some application and pass the XML file as a parameter to it...

The problem is, you're treating rt as a function/method, not an object. Runtime has the an exec method used to execute external commands, for example...

Runtime rt=Runtime.getRuntime();
String file="C:\\Users\\V\\Documents\\diplwmatiki\\SFBs\\SF_Antivalent.xml";
Process p=rt.exec(new String[]{
    "C:\\Windows\\System32\\java.exe", 
    "-jar", 
    "C:\\Users\\V\\Documents\\uppaal-4.0.13-aca\\uppaal-4.0.13\\uppaal.jar", 
    file});

Also, each command or argument you want to send to this external process must be it's own element within the array you pass to this method

This means tha "V\\\\Documents\\\\uppaal-4.0.13-aca\\\\uppaal-4.0.13\\\\uppaal.jar" + file won't actually have the effect you think it will.

I'd also recommend that you use ProcessBuilder over Runtime#exec , but that's me.

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