[英]How to control folder structure when exporting a Runnable JAR in Eclipse?
[英]How to maintain folder structure while exporting to Runnable Jar in Eclipse?
我試圖在過去一個星期中為此找到解決方案,並發布了與此有關的問題。
我創建了一個簡單的Maven項目。 並編寫了一個功能文件,即打開瀏覽器,轉到Facebook,然后關閉瀏覽器。
首先,下面是項目結構,
以下是我的功能文件。 功能文件的名稱是Testing.feature
Feature: Open FB
Scenario: Open FB
Given User opens "facebookURL" on "ChromeBr"
When User is on facebook
Then close the browser
然后,我為上述功能文件編寫了stepdefinition。 步驟定義文件的名稱為Testing.java
package stepDefinitions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import util.WebConnector;
public class Testing {
WebConnector wc = WebConnector.getInstance();
@Given("^User opens \"([^\"]*)\" on \"([^\"]*)\"$")
public void user_opens_on(String URL, String Browser) throws Throwable {
wc.openBrowser(Browser);
wc.navigateURL(URL);
System.out.println("Browser Opened & navigated to FB");
}
@When("^User is on facebook$")
public void user_is_on_facebook() throws Throwable {
System.out.println("User is on FB");
}
@Then("^close the browser$")
public void close_the_browser() throws Throwable {
wc.quitBrowser();
System.out.println("Browser Closed");
}
}
config.properties僅包含一個屬性
facebookURL=https://www.facebook.com
我編寫了一個公共類文件WebConnector.java ,它將具有用於初始化屬性文件的構造函數以及一些方法,例如打開瀏覽器和URL等。
package util;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebConnector {
public Properties OR = null;
public Properties CONFIG = null;
public static WebDriver driver;
static WebConnector w;
private WebConnector() {
if(CONFIG==null) {
try {
CONFIG = new Properties();
FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"\\src\\test\\java\\config\\config.properties"); **//Here only I get error when I export as Runnable Jar and Run**
CONFIG.load(fis);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static WebConnector getInstance() {
if(w==null)
w=new WebConnector();
return w;
}
public void openBrowser(String browserName) throws IOException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
public void navigateURL(String URL) throws IOException {
driver.get(CONFIG.getProperty(URL));
}
public void quitBrowser() {
driver.quit();
}
}
這是我的測試運行程序類,具有一個主要方法。 它可以使用Junit以及Java應用程序運行
package util;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "classpath:feature",
glue = "stepDefinitions"
)
public class RunCukesTest {
public static void main(String[] args) {
JUnitCore.main("util.RunCukesTest");
}
}
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Jar_Testing</groupId>
<artifactId>Jar_Testing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>util.RunCukesTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
問題:當我使用Junit或Java應用程序運行RunCukesTest.java時,它運行完美。 也就是說,它將打開瀏覽器,轉到fb並關閉瀏覽器。
但是,當我創建一個可執行的Jar並運行時,它沒有按預期運行。
我通過以下步驟導出為Runnable Jar:
1) Run--> Run Configuration--> Java Application-->New Launch Configuration-->And selects the main class as RunCukesTest.java-->Apply
2) Right click the project-->Export
3) Java--> Runnable JAR File -->Next
4) Under Launch Configuration select the RunCukesTest.java and give the export destination
5) And I have selected the option "Extract required Libraries into Jar"
5) click finish
假設我已將此Jar保存在我的桌面中。 桌面路徑->“ C:\\ Users \\ PC \\ Desktop”當我從命令提示符運行此Jar時,它顯示以下內容
C:\Users\PC\Downloads>java -jar Maven.jar
JUnit version 4.12
.java.io.FileNotFoundException: C:\Users\PC\Downloads\src\test\java\config\c
onfig.properties (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at util.WebConnector.<init>(WebConnector.java:19)
at util.WebConnector.getInstance(WebConnector.java:30)
at stepDefinitions.Testing.<init>(Testing.java:9)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct
orAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC
onstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(Defau
ltJavaObjectFactory.java:41)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJav
aObjectFactory.java:33)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.j
ava:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java
:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.ja
va:102)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
at util.RunCukesTest.main(RunCukesTest.java:16)
.EEII
←[31mFailed scenarios:←[0m
←[31mfeature/Testing.feature:3 ←[0m# Scenario: Open FB
1 Scenarios (←[31m1 failed←[0m)
3 Steps (←[31m1 failed←[0m, ←[36m2 skipped←[0m)
0m0.314s
java.lang.IllegalStateException: The driver executable does not exist: C:\Users\
PC\Downloads\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:53
4)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(Driv
erService.java:136)
at org.openqa.selenium.remote.service.DriverService.findExecutable(Drive
rService.java:131)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDrive
rService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExe
cutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(Driver
Service.java:329)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(C
hromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
at util.WebConnector.openBrowser(WebConnector.java:36)
at stepDefinitions.Testing.user_opens_on(Testing.java:12)
at ?.Given User opens "facebookURL" on "ChromeBr"(feature/Testing.featur
e:4)
Time: 0.359
There were 2 failures:
1) Given User opens "facebookURL" on "ChromeBr"(Scenario: Open FB)
java.lang.IllegalStateException: The driver executable does not exist: C:\Users\
PC\Downloads\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:53
4)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(Driv
erService.java:136)
at org.openqa.selenium.remote.service.DriverService.findExecutable(Drive
rService.java:131)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDrive
rService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExe
cutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(Driver
Service.java:329)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(C
hromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
at util.WebConnector.openBrowser(WebConnector.java:36)
at stepDefinitions.Testing.user_opens_on(Testing.java:12)
at ?.Given User opens "facebookURL" on "ChromeBr"(feature/Testing.featur
e:4)
2) Scenario: Open FB
java.lang.IllegalStateException: The driver executable does not exist: C:\Users\
PC\Downloads\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:53
4)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(Driv
erService.java:136)
at org.openqa.selenium.remote.service.DriverService.findExecutable(Drive
rService.java:131)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDrive
rService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExe
cutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(Driver
Service.java:329)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(C
hromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
at util.WebConnector.openBrowser(WebConnector.java:36)
at stepDefinitions.Testing.user_opens_on(Testing.java:12)
at ?.Given User opens "facebookURL" on "ChromeBr"(feature/Testing.featur
e:4)
FAILURES!!!
Tests run: 2, Failures: 2
我了解它正在嘗試查找文件,但失敗了。 當我打開Jar時,它在下面的結構中。
如何保持相同的文件夾結構,以免出現“找不到文件異常”或是否有其他方法可以成功運行此文件?
這不是jar的結構問題,而是關於讀取位置以查找文件的問題。
假設您有一個簡單的java文件,其代碼如下所示:
現在,如果您正在從IDE(eclipse)運行Main
類,則確實有"src"
文件夾和所有必需的sub folders
。 因此,不會有例外。
但是,如果您創建一個jar,並將該jar放在另一個“新文件夾”(例如jarFolder)中,那么當您運行代碼時,您會看到找不到異常目錄。 那是顯而易見的 。
因此,這取決於您,只有您來遵循所需的方法。
1)有些附帶jar的properties / config文件(當配置文件的內容是動態的並且需要更改時,建議不要這樣做。)
2)有些人將其配置文件保留在配置層下,然后無論您在哪里復制jar,都創建一個config文件夾並將配置文件放在那里。 在這種情況下,您可能需要閱讀
System.getProperty("user.dir")+File.separator+"config"+File.separator+"config.properties"; // Offcourse, using \\\\ is a bad way to use File.separtor as it will not work on unix again
3)另一件事,我建議人們不要購買他們的“ src”,而是運送通常駐留在classes文件夾而不是src文件夾中的二進制文件。 因此,應避免將src文件夾中的任何內容放入二進制jar中。
我的建議是,您可以使用批處理文件運行jar文件。
首先,您可以使用maven build創建項目的jar文件。 以下是步驟
右鍵單擊Maven項目->運行方式-> Maven構建->目標,給出干凈的程序包 ->運行
成功執行后,將在項目的目標文件夾中創建jar文件。 現在在您的項目位置創建一個批處理文件,例如run.bat
。 下面是您需要在批處理文件中編寫的代碼
java -Xms512m -Xmx512m -jar execute.jar -0 true
pause
您可以更改jar名稱。 現在轉到批處理文件位置,然后雙擊它。 您的項目將執行,控制台報告將顯示在命令提示符下。
我們無法維持項目的相同結構。 我發現最好的解決方案是,從其他任何路徑運行可執行jar時,我確保這些文件夾(不是jar的一部分,而是項目的一部分)與我的可執行jar所在的路徑相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.