0

I have created a login window with two textboxes for username and password and two command buttons for login and cancel..The window closed after pressing the cancel button. The login credentials should be passed as parameters to the powershell script to login the web site.. I cannot link powershell and java swing code..

My java swing code is given:

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.FileReader;
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 = 220;
                    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.lightGray);
                    g2d.drawString("Get Credential", 60, 80);
                }
            }

        };

        setContentPane(panel);
        setLayout(new FlowLayout());
        placeComponents(panel);

    }

    public 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);
        userText.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        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);
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });

        panel.add(loginButton);

        JButton cancelButton = new JButton("cancel");
        cancelButton.setBounds(220, 180, 80, 25);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        panel.add(cancelButton);
    }

    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);
            }
        });
    }
}

The powershell script is this:

$url = "https://www.jabong.com/customer/account/login/"
$username = $cred.username
 $username = $username.Replace("\", "")
$password = $cred.GetNetworkCredential().password
$ie = New-Object -com internetexplorer.application; 
$ie.visible = $true; 
$ie.navigate($url); 
while ($ie.Busy -eq $true) 
{ 
    Start-Sleep 1; 

} 
$ie.Document.getElementById("LoginForm_email").value = $username
$ie.Document.getElementByID("LoginForm_password").value=$password
$ie.Document.getElementByID("qa-login-button").Click();
4
  • What exactly is your question? Commented Jul 31, 2014 at 11:01
  • I need to enter credentials through the java swing window and after pressing the login button powershell code should run.. The value of username and password swing window need to passed to the powershell code and invoke login of the website..How can call powershel code with java code and pass java parameters to it Commented Jul 31, 2014 at 11:40
  • Looks to me like all the Java program does is prompt the user for his/her credentials. Is there any particular reason why you can't simply use Get-Credential in the PowerShell script and drop the Java program altogether? Commented Aug 4, 2014 at 8:48
  • Yes my requirement is to create a custom swing window for login Commented Aug 5, 2014 at 5:01

1 Answer 1

1

There are several ways you could approach this. For instance you could run the PowerShell script with username and password as arguments:

Process process = new ProcessBuilder(
                    "powershell.exe",
                    "-File", "C:\\path\\to\\your.ps1",
                    "-Username", userText.getText(),
                    "-Password", passwordText.getText()
                  ).start();

and make $Username and $Password parameters of the script:

[CmdletBinding()]
Param(
  [Parameter()][string]$Username,
  [Parameter()][string]$Password
)

$url = 'https://www.jabong.com/customer/account/login/'
$ie  = New-Object -COM internetexplorer.application
...

This would be bad practice, though, because that way the password would appear in clear text in the process list.

A somewhat better option would be passing username and password as environment variables:

ProcessBuilder pb = new ProcessBuilder(
                      "powershell.exe",
                      "-File", "C:\\path\\to\\your.ps1"
                    );
Map<String, String> env = pb.environment();
env.put("USER", userText.getText());
env.put("PASS", passwordText.getText());
Process process = pb.start();

which could be accessed in the script like this:

$username = $env:USER
$password = $env:PASS

Or you could do it the other way around and run the Java program from the PowerShell script:

$username, $password = & java -jar '.\Credential.jar'

and write the credentials to stdout in your Java program:

System.out.println(userText.getText());
System.out.println(passwordText.getText());

Note that either way you need to make userText and passwordText instance variables of the class and change placeComponents() to a non-static method.


With that said, if Java/Swing isn't a hard requirement, using Windows forms would also provide a way to build a custom dialog (one that will allow you to keep dialog and script code in the same file):

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function New-Button($label, $action, $width, $height, $top, $left) {
  $btn = New-Object System.Windows.Forms.Button
  $btn.Location = New-Object System.Drawing.Size($left, $top)
  $btn.Size = New-Object System.Drawing.Size($width, $height)
  $btn.Text = $label
  $btn.Add_Click($action)
  $btn
}

function New-Label($label, $width, $height, $top, $left) {
  $lbl = New-Object System.Windows.Forms.Label
  $lbl.Location = New-Object System.Drawing.Size($left, $top)
  $lbl.Size = New-Object System.Drawing.Size($width, $height)
  $lbl.Text = $label
  $lbl
}

function New-Input($width, $height, $top, $left, $mask=$false) {
  $input = New-Object System.Windows.Forms.TextBox
  $input.UseSystemPasswordChar = $mask
  $input.Location = New-Object System.Drawing.Size($left, $top)
  $input.Size = New-Object System.Drawing.Size($width, $height)
  $input
}

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(250, 160)

$form.Controls.Add((New-Label 'Username:' 60 20 20 10))
$user = New-Input 150 20 20 70
$form.Controls.Add($user)

$form.Controls.Add((New-Label 'Password:' 60 20 50 10))
$pass = New-Input 150 20 50 70 $true
$form.Controls.Add($pass)

$form.Controls.Add((New-Button 'OK' {$form.Close()} 70 23 85 70))
$form.Controls.Add((New-Button 'Cancel' {$user.Text=''; $pass.Text=''; $form.Close()} 70 23 85 150))

[void]$form.ShowDialog()

$username = $user.Text
$password = $pass.Text
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.