【質問】
実行時エラーの対処方法、代替方法。
【求動作】
propertiesファイルにクラス名を記載。
↓
実行時にpropertiesファイルから読み取り、対象をインスタンス化。
(根元にあるやりたい事は、seleniumを利用したブラウザテスト実行時、
対象ブラウザ毎のドライバークラスを設定ファイルで制御する事です)
【エラー内容】
eclipseからの実行時はエラーが発生しません。
jar作成の上、コマンドプロンプトから実行で以下のエラーが発生します。
java.lang.ClassNotFoundException: org.openqa.selenium.ie.InternetExplorerDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at jp.captureAutomationPrj.core.App.main(App.java:40)
【環境】
OS:Windows7 64bit
IDE:eclipse4.5
【コメント】
コンパイル周りの問題の場合、当方はコンパイル、ビルドはIDE頼りの為、知識に乏しいです。
この問題の場合、原因に加えIDEでの設定方法をご教授頂けると幸いです。
(もちろん、IDEの方はおまけ程度に考えてくださって結構です)
実行時に対象のjavaファイルへclasspathを通せば出来そう?な気もしますが、なるべく1個のjarにまとめてしまいたいです。
以下、テスト処理クラスとプロパティ管理クラスのソースです。
import周りは基本的にIDEの自動追加の為、省略しますが、
org.openqa.selenium.*のみ全て指定しています。
(ドライバークラスがあるところ)
【ソース-テスト処理クラス-】
public class App { public static void main(String[] args) { try { // プロパティ取得 CaptureProperties cp = CaptureProperties.getInstance(); Properties p = cp.getProperties(); // exePath設定 // ChromeとIEは設定しないとエラーになる if (p.getProperty(CaptureProperties.BROESER).equals("ie") || p.getProperty(CaptureProperties.DRIVER_NAME).equals("chrome")) { File file = new File("resource/" + p.getProperty(CaptureProperties.EXE_NAME) + ".exe"); System.setProperty("webdriver." + p.getProperty(CaptureProperties.BROESER) + ".driver", file.getAbsolutePath()); } // 対象ブラウザのドライバーを設定 Class driverClass = Class.forName(p.getProperty(CaptureProperties.DRIVER_NAME)); WebDriver driver = (WebDriver) driverClass.newInstance(); // Wait設定 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // テスト処理 // URLリスト分繰り返す Urls ul = new Urls(); for (URL url : ul.getUrlList()) { // 指定のURLへ接続する driver.get(url.toString()); // 指定の属性を探す(見つからない場合、wait設定値だけ待つ) driver.findElement(By.className("copyright")); // 保存先 File saveAddress = new File(p.getProperty(CaptureProperties.SAVE_ADDRESS)); if (!saveAddress.isDirectory()) { throw new Exception("保存先が存在しません。\n設定ファイルを再確認してください。"); } // ファイル名 String[] str = url.getFile().split("/"); String fileName = str[str.length - 1] + ".png"; // キャプチャ(バッファ→書き出し) BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(image, "png", new File(saveAddress, fileName)); } // 終了 driver.quit(); } catch (Exception e) { e.printStackTrace(); } } }
【ソース-プロパティファイル管理クラス-】
public final class CaptureProperties { // プロパティから呼び出す為のキー群 // 保存先 public static final String SAVE_ADDRESS = "saveAddress"; // 対象ブラウザ public static final String BROESER = "browser"; // exe public static final String EXE_NAME = "exeName"; // ドライバー public static final String DRIVER_NAME = "driverName"; // プロパティ private Properties properties = new Properties(); // インスタンス private static final CaptureProperties instance = new CaptureProperties(); private CaptureProperties() { try { load(); } catch (Exception e) { e.printStackTrace(); } } /** * インスタンス取得 * @return CaptureProperties */ public static CaptureProperties getInstance() { return CaptureProperties.instance; } // 設定ファイルのロード public void load() throws Exception { properties.clear(); // プロパティファイルの場所 File file = new File("config/application.properties"); // ファイルの存在確認 if (!file.isFile()) { // ファイルが存在しない場合は処理終了 throw new FileNotFoundException(file.getAbsolutePath()); } try (InputStreamReader in = new InputStreamReader(new FileInputStream(file), "utf-8")) { properties.load(in); } } /** * プロパティ取得 * @return Properties */ public Properties getProperties() { return properties; } }
【propertiesファイル】
#保存先 saveAddress = #ブラウザ ie || chrome 以外は設定不要 browser = ie #browser = chrome #エクゼ IEDriverServer || chromedriver 以外は設定不要 exeName = IEDriverServer #exeName = chromedriver #ドライバー 使いたいブラウザのドライバーを設定 driverName = org.openqa.selenium.ie.InternetExplorerDriver #driverName = org.openqa.selenium.chrome.ChromeDriver; #driverName = org.openqa.selenium.firefox.FirefoxDriver
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。