我正在尝试从Java代码执行程序.这是我的代码:
public static void main(String argv[]) {
try {
String line;
Process p = Runtime.getRuntime().exec(
"/bin/bash -c ls > OutputFileNames.txt");
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch (Exception err) {
err.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我的操作系统是Mac OS X 10.6.
如果我"> OutputFileNames.txt"从getRuntime().exec()方法中删除,所有文件名都打印在控制台上就好了.但我需要将它打印到文件中.
另外,如果我将命令更改为:
Process p = Runtime.getRuntime().exec(
"cmd \c dir > OutputFileNames.txt");
Run Code Online (Sandbox Code Playgroud)
并在Windows上运行它,它运行并在文件中完全打印结果.
我已经阅读了其他从Java执行另一个应用程序的帖子,但似乎没有与我的问题有关.
我真的很感激我能得到的任何帮助.
谢谢,
我正在尝试使用以下代码片段来编组消息:
JAXBContext jContext = JAXBContext.newInstance(Iq.class);
Marshaller m = newJAXBContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Bind bind = new Bind();
bind.setResource("resource");
Iq iq = new Iq();
iq.setId(iqId);
iq.setType("set");
iq.getAnies().add(bind);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
m.marshal(iq, baos);
Run Code Online (Sandbox Code Playgroud)
这里,Iq和Bind是由相关的xmpp模式形成的对象.我的问题是,使用jaxb 2.0及更高版本,所有命名空间都在根元素中声明:
<iq from='juliet@example.com/balcony'
id='rg1'
type='get' xmlns='jabber:client' xmlns:ns1='urn:ietf:params:xml:ns:xmpp-bind'>
<ns1:bind>
<ns1:resource>resource</ns1:resource>
</ns1:bind>
</iq>
Run Code Online (Sandbox Code Playgroud)
但是,这里需要的是命名空间应该占用适当的位置:
<iq from='juliet@example.com/balcony'
id="rg1"
type="get" xmlns="jabber:client">
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
<resource>resource</resource>
</bind>
</iq>
Run Code Online (Sandbox Code Playgroud)
有没有办法通过JAXB 2.0或更高版本在第二个xml节中看到它们来编组xmpp节?
长话短说,我在这里有两个问题:1.在适当的位置声明命名空间.2.删除我理解的名称空间前缀可以使用NamespacePrefixMapper删除?但不确定,一个例子会很棒.