Program works from Eclipse but not from executable JAR file - java

Below is my code which works perfect in Eclipse Java Project
String IEPath = "src/IEDriverServer.exe";
File file = new File(IEPath);
System.setProperty("webdriver.ie.driver",file.getPath());
WebDriver driver = new InternetExplorerDriver();
If I export the same code to a runnable JAR file and double click it or if I run it from command prompt gives below exception
The driver ececutable does not exist C:\Backup\New folder\src\IEDriverServer.exe
I have copied IE exe inside my Java Project and have exported the Java Project including the IE exe. When I run the JAR, it is failing to pick the IE exe path.
Please help! TIA!

You have two options here:
Read the resource inside the jar. See more info here.
Use WebDriverManagerto automate the management of IEDriverServer.exe.
For alternative 2, simply import the WebDriverManager library in your project and change your code:
String IEPath = "src/IEDriverServer.exe";
File file = new File(IEPath);
System.setProperty("webdriver.ie.driver",file.getPath());
... by:
InternetExplorerDriverManager.getInstance().setup();

Related

execute a .jar file from C# but it generates files at wrong location

I have coded a Minecraft Server Runner in C# WinForms which lets you run a Minecraft Server, a .jar file which needs to generate files. The problem is that I launch this .jar file via the .exe application, and the files generate at the .exe application location.
-- What I have tried:
I tried moving the .exe application to the specific server file location, but the application needs a restart to register this change which I don't want to happen.
I also don't want the user being forced to put the .exe application to the Server folder and restart it. Here is the code I use to launch the .jar file:
Process.Start("C:\user\documents\server\server.jar");
How can I fix this issue?
The jar file can be executed by the java -jar filename.jar. So use the following Process.Start call to invoke the jar.
Process.Start("java", "-jar C:\user\documents\server\server.jar", username, password, domain);
Hope this helps.
P.S: For this to work, either add the Java to your path or invoke with the java.exe's Path.
To fix this I executed the .jar file in the C# application via the CMD.
Here is the code I used instead:
string path = #"C:\user\documents\server\"; //Path to your server.jar file.
var process = new System.Diagnostics.Process();
process.StartInfo.FileName = path + "server.jar"; //Name of the .jar file.
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
process.Start();
All credits go to "Olivier Rogier" ( https://stackoverflow.com/users/12031933/olivier-rogier ) for helping me find this solution

Webdriver : ChromeDriver : Accessing From Within Framework Jar File

I am developing a framework using selenium webdriver in Java. I have two maven projects. One is for the framework and other is for the test project.
In order to launch launch ChromeDriver, I need to set the system property with the path of the chromedriver.exe file. I am doing this in DriverFactory.java which is in /src/main/java of the Framework Project. Now, if I place the exe files in src/main/resources/drivers, Java complains that the file is not found. The code is :
private static String chromeDriverLocation = "drivers/chromedriver.exe";
File cDriver = new File(DriverFactory.class.getResource(chromeDriverLocation).getFile());
// Is it executable
if (!cDriver.canExecute()) {
cDriver.setExecutable(true);
}
System.setProperty("webdriver.chrome.driver", DriverFactory.class.getResource(chromeDriverLocation).getFile());
I tried placing this file in src/main/resources/drivers in the Test Project and modified the code like :
private static String chromeDriverLocation = System.getProperty("user.dir")+ "\\classes\\drivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
Though the above piece of code works in local when executed through Jenkins(Maven), I am not sure if this is the best way. Can someone suggest other good ways of achieving this ?
As an option, you may place the chromedriver.exe file into a directory added to PATH. In this case running the ChromeDriver will be as simple as creating an instance of new ChromeDriver() without dealing with system properties.
If you still would like to store chromedriver.exe under Resources, you may get it as steam, save to a file and set the "webdriver.chrome.driver" property:
InputStream in = getClass().getResourceAsStream("chromedriver.exe");
OutputStream out = new FileOutputStream("chromedriver.exe");
IOUtils.copy(in, out);

Runnable jar exported with eclipse isn't working, in eclipse it still works

I'm trying to export a java project in eclipse as a runnable jar, but for some reason the runnable jar doesn't work. If I double click the executable jar, it doesn't do anything. I tried both extract and package required libraries into generated jar.
So I also tried to export some simpler projects, those worked fine. The biggest difference is my real project has files: images and xml files.
In code reference them like this:
File file = new File("Recources/test.xml");
ImageIcon imageIcon = new ImageIcon("Recources/" + num + ".gif");
The structure of the project looks like this:
But in the executable jar they look like this:
Thank you for your help.
Edit:
I have tried the 'java -jar filename.jar', but now it says it can't find my resources folder, while in eclipse it can still find it.
Files in a JAR-File aren't just like files stored in your hard-disc. If you include files in a JAR, they'll be seen as a Stream of Bytes. So you have to use different methods to access these resources.
//To read/access your XML-File
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/test.xml")));
//To read/access your gif-Files
ImageIcon icon = new ImageIcon(this.getClass().getResource("/"+num+".gif"));
"/" is not the root-Folder of your file-system, but the root folder of the resources inside your JAR.
The issue may be that Java is not the default program to run the jar.
Try right click -> Open with, and select the Java Runtime, and it should run successfully.
Make it the default program to enable double-click running.
Right click -> Properties -> Change -> C:\Program Files\Java\jre7\bin\javaw.exe
Inspired by stratwine's answer at https://stackoverflow.com/a/8511277
So thank you all, but it seems like the problem wasn't the export only. There was an error I saw when I opened my program with cmd, I was using file name to open xml and images while I should have used inputStreams: https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html.

ireport not working after making executable

I am developing a point of sales system in java using ireport as my reporting tool, which works fine in the IDE (Netbeans), but after packaging with install anywhere the ireport does not work anymore. Can anyone help on which library I might have excluded. Or where I am mistaken
Your executable jar file not working because it can't find its dependent jar file any more in the packaging. You can also get the exact exception by running application by cmd. like:
cmd -> open directory which contain your jar file -> write jarfile.jar
You have to do following steps:
open properties of your project
click on build => packaging
than you must check on copy dependent liraries
output package will pack your project in jar file and libraries in lib folder
you must move your lib folder as well if your moving your jar file
I've made a temporary solution to this problem about iReport/jasper report not opening/making an executable.
Seems that the program cannot locate the .jrxml/.jasper file in its default location (within the project folder usually with the build and manifest file) so the alternative would be to save the report file somewhere in the hard disk. ex. "D:/Files and Documents/Documents/report1.jrxml"
This works but somehow not the proper way.
After you design your report in jrxml, then compile it
(click that icon).
then change your jrxml report to jasper, like this.
Map<String, Object> nim = new HashMap<String, Object>();
nim.clear();
Statement stmt2 = SGB.c.createStatement();
ResultSet rs = stmt2.executeQuery("select * from mytable;");
JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper", nim, new JRResultSetDataSource(rs));
JasperViewer jv = new JasperViewer(jasperPrint);
formReport fformReport = new formReport(mainform,true);
fformReport.setBounds(jv.getBounds());
fformReport.getContentPane().add(jv.getContentPane());
fformReport.setLocationRelativeTo(null);
fformReport.setVisible(true);

Reading resource from jar isn't working as expected

I'm attempting to create an executable jar for a selenium test. Part of the things the code needs to do is set a system property to tell Selenium where the driver executable can be found (I'm using the chromedriver). File structure is as follows:
src
com
mycompany
SeleniumTest.java
chromeDriver
windows
chromedriver.exe
And the code is as follows:
private static String WINDOWS_DRIVER = "/chromeDriver/windows/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",
SeleniumTest.class.getResource(WINDOWS_DRIVER).getFile());
When executed in eclipse, this code works fine. However, when I export to a runnable jar file (from eclipse) I get the following error:
Exception in thread "main" java.lang.IllegalStateException: The driver executable
does not exist: F:\temp\file:\F:\temp\seleniumTest.jar!\chromeDriver\windows\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:117)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:112)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:75)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:139)
And yet seleniumTest.jar exists at F:\temp as does the path within the jar which the error message specifies.
Any ideas on what is wrong or suggestions to try? I've tried changing the slahses to backslashes and also (just as a test) hard coding the path (e.g. setting the system property to F:\temp\seleniumTest.jar!\chromeDriver\windows\chromedriver.exe), but neither has worked.
The system property is supposed to contain the path to the file, on the file system, where the driver can be found and executed.
The driver is not a file. It's an entry of your jar file. Executables bundled in a jar file can't be executed.
If you really want to bundle the driver into your jar file and execute it, then you'll have to read the bytes from this classpath resource, write them to a temporary executable file, and then tell selenium where this temporary executable file is located.
Try something like:
// locate chromedriver in the jar resources
URL res = getClass().getResource("/chromeDriver/windows/chromedriver.exe");
// locate chromedriver in the jar filesystem
File f = new File(res.getFile());
// copy chromedriver out into the real filesystem
File target = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + f.getName());
java.nio.file.Files.copy(f.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (!target.canExecute())
throw new FileNotFoundException("chrome.exe copy did not work!");
System.setProperty("webdriver.chrome.driver", target.getCanonicalPath());

Categories

Resources