ボタンが 1 つある簡単なテスト プログラムがあります。ユーザーがボタンをクリックすると、プログラムは Runnable JAR を作成することになっています。Runnable JAR は、Firefox で google.com を開く単純なプログラムです。プログラムには3つのクラスがあります。
1) メイン.java
package test;
public class Main {
public static void main(String[] args) {
Selenium.getGoogle();
}
}
2) セレン.Java
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium {
public static void getGoogle () {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
}
}
3) TestGUI.java
package test;
import javax.swing.*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public TestGUI() {
setSize(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
* JButton.
*/
JButton startButton = new JButton("Create Runnable JAR file ! ");
add(startButton);
startButton.addActionListener(this);
}
public static void main(String[] args) {
new TestGUI();
}
public void actionPerformed(ActionEvent e) {
System.out.println("The Button Works");
String file1ToCompile = "test" + java.io.File.separator + "Main.java";
String file2ToCompile = "test" + java.io.File.separator + "Selenium.java";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, file1ToCompile, file2ToCompile);
if (compilationResult == 0) {
System.out.println("Compilation is successful");
} else {
System.out.println("Compilation Failed");
}
}
}
私は 2 つの主要なクラスを持っています。TestGUI.java
GUIが表示されるようにマニフェストに追加しました。ユーザーがボタンをクリックしたら、プログラムで と で構成される Runnable JAR を作成する必要がMain.java
ありSelenium.java
ます。
ただし、エラーが発生null pointer exception
しますint compilationResult = compiler.run(null, null, null, file1ToCompile, file2ToCompile);
これどうやってするの?