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);
}
});
}
}
나는 또한 내가 자바 클래스를 사용하여 PowerShell을 호출하는 JNI를 사용할 수 있는지 여부를 알고 싶습니다.. 내가 점점 오전 오류를 클릭하는 동안 로 :
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)`
오류 스트림도 확인하십시오. 내 추측은 실행 정책 일 것입니다. – Joey