简体   繁体   English

如何通过Java在.jar中打开文件

[英]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. 我想在.jar应用程序中打开文件,并且我想使用Java来做到这一点。 Explaining, for example I have the file SF_Antivalent.xml and I want to open it with uppaal.jar. 例如,解释一下,我有文件SF_Antivalent.xml,我想用uppaal.jar打开它。 How do I do this using Java. 我如何使用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. 我收到此错误:类型测试的rt(String,String,String)方法未定义。 Is there something to do? 有什么事要做吗?

The reason you are getting the error is because rt is a Runtime object, not a method. 收到错误的原因是因为rt是Runtime对象,而不是方法。 To call a method of rt do this: 要调用rt方法,请执行以下操作:

rt.someMethodName();

With the code above you cannot get XML file, you're trying to execute something, instead of opening file inside JAR archive 使用上面的代码,您无法获取XML文件,而是尝试执行某些操作,而不是在JAR存档中打开文件

Look into getResourceAsStream , this will give you possibility to load any file from JAR 查看getResourceAsStream ,这将使您有可能从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... 您的问题有点令人困惑,但是,我相信您想运行一些应用程序并将XML文件作为参数传递给它...

The problem is, you're treating rt as a function/method, not an object. 问题是,您将rt视为函数/方法,而不是对象。 Runtime has the an exec method used to execute external commands, for example... Runtime具有exec方法,用于执行外部命令,例如...

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. 这意味着"V\\\\Documents\\\\uppaal-4.0.13-aca\\\\uppaal-4.0.13\\\\uppaal.jar" + file实际上不会产生您认为会的效果。

I'd also recommend that you use ProcessBuilder over Runtime#exec , but that's me. 我还建议您在Runtime#exec使用ProcessBuilder ,但这就是我。

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

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