我正在尝试使用java调用powershell脚本..我的代码无效... 我创建了一个登录窗口。单击登录按钮时,需要调用powershell脚本...
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Credential extends JFrame {
private static final long serialVersionUID = 1L;
public Credential()
{
setUndecorated(true);
setBackground(new Color(0,0,0,0));
setSize(new Dimension(400,300));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel()
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (g instanceof Graphics2D)
{
final int R = 250;
final int G = 220;
final int B = 250;
Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
0.0f, getHeight(), new Color(R, G, B, 255), true);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
Font font = new Font("Serif", Font.PLAIN, 45);
g2d.setFont(font);
g2d.setColor(Color.red);
g2d.drawString("Get Credential",60,80);
}
}
};
//Container contentPane = Jframe.getContentPane();
//contentPane.setLayout(new FlowLayout());
setContentPane(panel);
setLayout(new FlowLayout());
placeComponents(panel);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("User");
userLabel.setBounds(40, 100, 80, 25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(130,100, 160, 25);
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(40, 140, 80, 25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(130, 140, 160, 25);
panel.add(passwordText);
JButton loginButton = new JButton("login");
loginButton.setBounds(100, 180, 80, 25);
runn();
panel.add(loginButton);
JButton cancelButton = new JButton("cancel");
cancelButton.setBounds(220, 180, 80, 25);
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// statusLabel.setText("abcd");
System.exit(0);
} });
panel.add(cancelButton);
}
public void runn() {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("powershell C:\\testscript.ps1");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
try {
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
reader.close();
proc.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
GraphicsEnvironment ge
= GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
boolean isPerPixelTranslucencySupported
= gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
if (!isPerPixelTranslucencySupported)
{
System.out.println("Per-pixel translucency is not supported");
System.exit(0);
}
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
Credential gtw = new Credential();
gtw.setVisible(true);
}
});
}
}
我还想知道是否可以使用jni使用java类调用powershell ..我收到的错误是:
Exception in thread AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method runn() from the type Credential
at Credential.placeComponents(Credential.java:82)
at Credential.<init>(Credential.java:57)
at Credential$2.run(Credential.java:136)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)`
答案 0 :(得分:0)
你是什么意思
我的代码无效?
你收到任何错误吗?如果是这样,那是什么。你可以发布你的异常追踪。
从您的代码Process proc = runtime.exec("powershell D:\\exportasxml.ps1");
您是否将powershell路径设置为环境变量?您需要确保正在执行的命令位于正确的路径中。