Selenium-extract WebDriver from jar for execution - java

I have a selenium program that is intended to be copied onto other computers before execution. I would like to only have to ship out a runnable jar. On execution the program should extract chromdriver.exe to a new directory in the directory from which the program was executed. The .exe should only be exported if it does not already exist in that exact location. Assuming I do already know the .exe is not there, how do I program the application to extract the .exe File? The resource folder has been added to the class path under sources (I am using eclipse) so the .exe does appear in the jar when I export the project. Now I just need to see how to extract it on execution
Thanks!

I assume that your chromedriver.exe file is contained in your jar file in a package named res. The following code will extract the file to your local harddrive.
InputStream i = this.getClass().getResourceAsStream("/res/chromedriver.exe");
Files.copy(i, new File("c:\\target\\chromedriver.exe").toPath());
You can add a CopyOption if you like.

Related

Absolute file paths when running program from jar?

I don't understand how Java jar files work. I am trying to understand what is possible and not possible when creating a Java jar file. Is it possible to have a String path running normally in a Java jar file? Will this normally work as it works when running main class in eclipse? I mean, I have an absolute path in my main class that grabs the file and reads from it.
public static final String file1 = "C:\\Users\\Documents\\test1.txt";
public static final String file2 = "C:\\Users\\Documents\\test2.txt";
This is what I have when running my program and it works fine. This is inside a class that is called somewhere along when I want to read a file. My question is... will this prevent my jar file from working properly normally AS when running the main class from eclipse?
I have the jar file but what if it doesn't or does it still look for file1 and file2?
It doesn't matter whether that code is in a jar file or not. The strings will still be exactly as they are, and if you pass them to methods that look for files with those paths, it'll look for files with those paths in the file system of the machine where the code is running. It won't look for them inside the jar file.
A Jar file is basically an executable of your project, it is used for example by frontend's who need a backend but don't want to open an IDE for compiling and executing purposes. Your Jar file contains .class files responsible for the execution of your project, you an execute your jar in a server too, so your application will run for more people (if you configure right).

Java - How do I copy a java aplication jar file?

Is it possible to copy a jar file from which I'm running my application?
How do I find a source to this jar file?
Each user can save my application's jar file in a different folder.
I can answer one part of your question: how do you find the location of your JAR at runtime:
File jarDir = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath());
locationOfJar = jarDir.getAbsolutePath();
This will give you the path to the folder the Jar is currently in as a String.
What IDE or Compiler are you using? If you are using Eclipse you should find where your saves go and check your package, there you'll find the .jar and the .java forms of your application.

FileNotFoundException for runnable jar with testng

I'm exporting runnable jar with a main method that reads testng xml and starts execution, however After exporting, the jar doesn't find xml path, but when I run it through eclipse, it executes fine.
It is searching for xml on the exported location of jar,instead it should be packed within jar itself.not sure why is so.
below is the error.
It is searching for xml on the exported location of jar
No it isn't. Java is looking for that file in the current working directory.
instead it should be packed within jar itself.not sure why is so.
So it should be accessed via the class-resource API, not via a FileInputStream.
I was also getting this problem. You have to place the 'files' in the directory where you have placed the jar file.
Do these changes:
1) Edit your code and write the file's name only where you have put the source location of the file.
2) Then export to .jar file.
3) Put those files (.xml) in the directory where you have placed the .jar file.
It worked for me! I hope it will work for you too.

How to execute a program within a Jar file

Soo my problem is, that I made some kind of launcher and wrote a method that is being called if you click on the "play" button.
private void PlayButtonActionPerformed(java.awt.event.ActionEvent evt) {
try
{
new ProcessBuilder("src/calc.exe").start();
}
catch (IOException ex)
{
Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
}
}
The file that is being executed is just a copy of the calculator from Windows and
is in the src folder of my project. (Path is "src/calc.exe")
Everything works fine AS LONG as I don't compile my netbeans project file into a JAR file.
If I run the JAR file and click on the play button - nothing happens.
I really hope you can tell me how to fix this.
(P.S.: calc.exe is not always the name of the program to be run. The filename is determined by an .ini.
calc.exe is only used to test the program.)
First, some context:
A JAR file is an archive, a package (compressed or not) that contains Java bytecode and other resources that can be consumed from inside a JVM.
An EXE file is executable binary file that can only be understandable by Windows/MS-DOS family OSs.
When you run your program without packaging it into a JAR file, all resources (the class file and the executable) are accessible from the OS. Windows will launch the JVM and from inside there you, by means of the ProcessBuilder, instruct the OS to invoke an executable file accessible in a path which is relative to the class performing the execution instruction. Until there, everything is fine and as you said, it works.
However, when you pack your Java class and the executable file in a JAR and invoke the JAR, its contents are no longer accessible for the OS. What is happening is that the OS will launch the JVM and it will process the JAR file to find the executable class (the one in which you invoke the ProcessBuilder). From that class file you now instruct the OS to launch an executable which is in a path relative to the JAR file instead of being relative to the class file performing the invoke instruction. Since the EXE is inside the JAR file instead of being relative to it, execution fails because it can't find the executable.
This is that way because the OS won't look inside the JAR file for a specific file, for the OS the JAR file is just that, a file, not a folder so it will ignore its contents.
So, the conclusion is that, since you are invoking the OS to perform a specific operation in another file, just must place that file outside the JAR and in a relative path to that JAR.
If for some reason you insist on packaging your EXE inside the JAR, then you need to extract it outside the JAR before invoking the ProcessBuilder to a temporary folder and invoke the ProcessBuilder using that new path.
This is sort of a touchy subject.
Whoever commented that JARs are not able to do this is wrong. To the contrary, it is applets that do not have such a permission unless they are signed and validated by the author through Oracle.
You said that it all works fine unless you put it in a JAR. That is probably because of your URI being relative to the Windows filesystem.
The way to usually get the file regardless of the packaging around the reference is to do something like this:
MainClass.class.getResourceAsStream(file);
The static getResourceAsStream will parse the available relative and absolute paths, and if you put your "src/calc.exe" here, it should work.
Another suggestion is to thread the process opening. If you have the program as a single thread, the Java program will hang until the external process is gone and cleaned up.

making .exe file for java project containing sqlite

I want to create .exe file for my java project and give it to my friend. I wrote my project in eclipse and it uses sqlite. I don't know how to make a .exe file that can be run in other PCs.
Can any one help?
P.S:I saw this link but it is not useful for me!
Create .jar of an eclipse project that uses Sqlite DB
.exe is a creature of Windows.
To run a Java program, you typically:
Create a .jar file (the "native" Java library format)
Write a DOS/Windows .bat file (or, equivalently, Linux shell script) to run your Java program (using the Java .jar file)
Optionally, create some easy-to-use mechanism for the end user to download the Java JRE (if it's not already installed on their PC).
Your .bat file can be as simple as this:
start javaw -jar myjarfile.jar
Have you considered creating Runnable jar from eclipse.
In eclipse, go to File > Export > Java > Runnable Jar.
There you ll find some options and you can use what suits you. The jar created should be able to run all by itself (obviously it needs the java run time).
Try this out.
I would recommend using a bat file. You can make a double clickable jar file, but I feel that is sometimes restrictive and not intuitive.
Not many end-users know that a jar file is double clickable.
You need to make sure the jar file has a main class and classpath defined. The classpath section in the jar file sometimes causes issues. Like you cannot reference a file or path on the file system. Only files or folders that can be relatively referenced from the location of the jar file.
For windows users, you cannot easily make an exe file from a jar file. There are methods like using jsmooth, that will wrap your jar file into an exe file (bloating the exe file in the process).
The easiest way is to create a bat file. You can easily convert a bat file into an exe and make the exe file have an icon and everything. Link to a converter here:
http://download.cnet.com/Bat-To-Exe-Converter/3000-2069_4-10555897.html
First create an executable jar file by clicking on File menu, then export, and then select runnable jar file.
Then select main class and click ok - the jar file will be created.
After that use Launch4j application to create .exe. Launch4j is the best option for creating an exe file.
Then use Inno Setup Creater to create an installer and it is done.

Categories