【VBS & PHP】スクリプトからスタンバイ状態にする で作成した Windows Script Components を Java から呼び出します ※ JCom224 と JACOB-1.14.3 によるテストです JCom (224)JCom は、こちら からダウンロードできます。 LGPL なので、配布元(ソースもあるので)を明示しておけば改造しないかぎり 再配布も問題無いでしょう。 以下のサンプルでは、Swing で画面を表示して実行しています ( 元々は、Exlipse+VisualEditor のスケルトンです ) import javax.swing.*; import java.awt.*; import jp.ne.so_net.ga2.no_ji.jcom.*; public class Main extends JFrame { private JPanel jContentPane = null; private JButton jButton = null; // ***************************************************** // ボタン作成とクリックイベント // ***************************************************** private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new Rectangle((300/2)-50, ((200-40)/2)-15, 100, 30)); jButton.setText("実行"); jButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); // 参照カウンタ管理クラス // この場で解放するので、VBScript の Dim だと思えばよいです ReleaseManager rm = new ReleaseManager(); try { // インスタンス作成 IDispatch com = new IDispatch(rm, "Wscript.Shell"); // 引数作成 String command = "notepad.exe"; int type = 1; Boolean wait = true; Object[] param = new Object[] { command, type, wait }; // メソッド実行( 必要な場合、戻り値も取得可能です ) com.method( "Run", param ); // インスタンス作成 IDispatch com2 = new IDispatch(rm, "Lbox.Standby"); // 引数作成 int second = 1; Object[] param2 = new Object[] { second }; // メソッド実行 com2.method( "Standby", param2 ); } catch ( Exception ex ) { ex.printStackTrace(); } finally { rm.release(); } } }); } return jButton; } // ***************************************************** // エントリポイント // ***************************************************** public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ SwingUtilities.invokeLater(new Runnable() { public void run() { Main thisClass = new Main(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } // ***************************************************** // コンストラクタ // ***************************************************** public Main() { super(); initialize(); } // ***************************************************** // 初期処理 // ***************************************************** private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("COM テスト"); } // ***************************************************** // 画面( パネル作成 ) // ***************************************************** private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJButton(), null); } return jContentPane; } } カレントに jcom.dll と jcom.jar を置きます ビルド・コマンドライン
javac -cp .;.\jcom.jar Main.java 実行・コマンドライン
java -cp .;.\jcom.jar Main JACOB (1.14.3)
JACOB は、こちら からダウンロードできます。 こちらも LGPL です。 JCom と使い方も似ていますが、こちらのほうがアップデートされて バージョンアップされていますし、複雑な COM にも対応できるような気がします。 ( 複雑なものは、本来 C++ で COM するか、このサンプルの WSC で良いと思いますが ) import javax.swing.*; import java.awt.*; import com.jacob.activeX.*; import com.jacob.com.*; public class Main extends JFrame { private JPanel jContentPane = null; private JButton jButton = null; // ***************************************************** // ボタン作成とクリックイベント // ***************************************************** private JButton getJButton() { if (jButton == null) { jButton = new JButton(); jButton.setBounds(new Rectangle((300/2)-50, ((200-40)/2)-15, 100, 30)); jButton.setText("実行"); jButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("actionPerformed()"); // インスタンス作成 ActiveXComponent com = new ActiveXComponent("Wscript.Shell"); ActiveXComponent com2 = new ActiveXComponent("Lbox.Standby"); try { // 引数作成 Variant command = new Variant((Object)"notepad.exe"); Variant type = new Variant((Object)1, true); Variant wait = new Variant((Object)true, true); Variant[] param = new Variant[] { command, type, wait }; // メソッド実行 com.invoke( "Run", param ); // 引数作成 Variant second = new Variant((int)1); Variant[] param2 = new Variant[] { second }; // メソッド実行 com2.invoke( "Standby", param2 ); } catch ( Exception ex ) { ex.printStackTrace(); } finally { com2.safeRelease(); com.safeRelease(); } } }); } return jButton; } // ***************************************************** // エントリポイント // ***************************************************** public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ SwingUtilities.invokeLater(new Runnable() { public void run() { Main thisClass = new Main(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true); } }); } // ***************************************************** // コンストラクタ // ***************************************************** public Main() { super(); initialize(); } // ***************************************************** // 初期処理 // ***************************************************** private void initialize() { this.setSize(300, 200); this.setContentPane(getJContentPane()); this.setTitle("COM テスト"); } // ***************************************************** // 画面( パネル作成 ) // ***************************************************** private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getJButton(), null); } return jContentPane; } } カレントに jacob-1.14.3-x86.dll と jacob.jar を置きます ビルド・コマンドライン
javac -cp .;.\jacob.jar Main.java 実行・コマンドライン
java -cp .;.\jacob.jar Main |