我有一个Java代码,在执行时会向用户显示一个GUI按钮,并在按下它时;将执行bash脚本。我的脚本在终端上运行,但是当我尝试通过Runtime.getRuntime()。exec运行Java脚本时,我收到了权限错误。我在命令中也使用了chmod 777,但都是徒劳的。
**MY Java code is**
package com.interface_saad;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.io.File;
/**
*
* @author Saad
*/
public class Main {
/**
* @param args the command line arguments
*/
static JFrame fr=new JFrame("QoS");
static JPanel button_pan=new JPanel();
static JButton QoSButton=new JButton("QoS");
public static void main(String[] args) throws IOException {
final File file = null;
button_pan.add(QoSButton);
fr.add(button_pan,BorderLayout.CENTER);
fr.setVisible(true);
fr.setSize(200,200);
QoSButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
System.out.println("path");
Runtime.getRuntime().exec("chmod 777 /home/talal/enable.sh");
Runtime.getRuntime().exec("./ /home/talal/enable.sh");
} catch (Exception ex) {
System.out.println(ex);
}
}
});
fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
}
}
,我的bash脚本是enable.sh
#!/bin/sh
(sleep 2
echo root
sleep 2
echo admin
sleep 1
echo '/usr/sbin/svqos status `get_wanface` 0'
sleep 2
echo '/usr/sbin/svqos `nvram get wshaper_downlink` `nvram get wshaper_uplink` `get_wanface` `nvram get wan_mtu` 0 '
sleep 1
echo '/usr/sbin/svqos status `get_wanface` 0'
sleep 4) | telnet 192.168.1.1 -l root
需要建议。我迫切需要帮助。