How to package XML with runnable Jar? - eclipse

I have a Java project with many testNG classes. In order to call the suite of test classes you need to run an XML file. I wrote a class in the same package as the XML which runs said XML. I'm exporting the project as a runnable jar that uses the main as the launch configuration.
public class mainTest {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
File file = new File("src\\xml\\whole.xml");
suites.add(file.getAbsolutePath());
testng.setTestSuites(suites);
testng.run();
}
}
My export steps:
Right click and export on the entire project
Select Runnable JAR File
Select "mainTest" as my launch configuration Export to Desktop
When I run this class directly in Eclipse, the XML is called fine. However, when I export the entire project as a runnable jar, I get this error.
It seems the jar is looking for the XML in the same directory as the jar (where it doesn't exist), but shouldn't the XML already be included inside the jar?

Related

Could not find main method from given launch configuration

I've a simple Java project that works when I execute it at Eclipse environment. But when I try to export it to a Runnable Jar, I get the following error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: JavaSwing/src.main.java/com/cansoft/GUIProgram.java
Exported with compile warnings: JavaSwing/src.main.java/com/util/Util.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
I read other posts which suggest to create a MANIFEST.MF file specifying the main-class which I did. It is placed at MyProjectFolder/META-INF/MANIFEST.MF and it contains the following information:
Manifest-Version: 1.0
Class-Path: resources
main-class: com.cansoft.GUIProgram
My main class is as follows:
public class GUIProgram {
private JFrame folderCreationSubappFrame;
private Color color;
private String home;
private final static Logger LOG_MONITOR = Logger.getLogger("com.cansoft");
public static void main(String[] args) {
try {
new GUIProgram();
} catch (Exception e) {
LOG_MONITOR.log(Level.INFO,e.getMessage());
}
}
public GUIProgram() throws InterruptedException, SecurityException, IOException {
home = System.getProperty("user.home") + File.separator + "Documents";
startLogSystem();
if(isFirstRun()) {
showWelcomeFrame();
} else {
initialize();
}
} .... More and more code
Does anybody know what am I missing? Any help much appreciated.
Thank you.
It is not enough to create the manifest file, you need to explicitly choose it in the Eclipse jar export dialog.
Answer to Comment
If you use "runnable jar", make sure that you chose the correct launch configuration and that the launch configuration successfully runs when chosing "Run As" -> "Run Configurations" -> "Java Application" -> Your Configuration -> "Run"
I finally find out where the problem was, it was quite simple btw. I had created my GUIProgram within a src.main.java package, but that package was created (my bad) as resources instead of folders, so Eclipse was smart enought to run it but when trying to generate the JAR which expected a correct java project structure, it was failing because truly there were not GUIProgram java class at src path (src was not folder type but resources).
Hope I succeed explaining.

OpenCV Exporting Runnable JAR in Eclipse

While Exporting Runnable JAR in Eclipse, Got error as no open cv in java.library.path.
Included Steps :-
Created User Library (ex OpenCV320) in eclipse and added in project build path also dll (as my system is 64 bit "C:\OpenCV\opencv\build\java\x64") opencv_java320.dll is set as Native library Location.
While exporting runnable jar selected "Extract required libraries into generated jar".
Here is the solution
No need to create separate user library.
Add below code.
String libraryPath = "C:\OpenCV\opencv\build\java\x64";
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
3.And export the runnable jar.

Export Selenium TestNG script from Eclipse to runnable jar

I have a script written in Selenium using the TestNG framework. This script executes via an xml file. Since we do not have main method in this, how can I export this script to a runable JAR file?
I marked this question as a dupe (and this code snippet comes from the dupe question), but basically you could write a public static void main method that scans for tests or loads a existing testng.xml file. Then, you just need to use something such as Maven Shade plugin to package it.
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { test_start.class });
testng.addListener(tla);
testng.run();
}
I used WebDriverManager project to manage my driver libraries. I would have the .jar load those externally.

Not able to create executable jar for webdriver+TestNg project which does not contain main method in any class

I am learning webdriver and I have created one project with TestNg.
I have several classes in my package under src folder.
No class contains public static void main(....). i.e[Entry Point]
My question is :
Can we create Runnable / Executable jar file through eclipse for projects like this[project without main method]. I searched on many sites but unfortunately didnt get solution.
Please suggest me some links OR The way by which we can do this.
To create a jar file of the TestNG without main method you have to create another class which contain main method.
Suppose you have a TestNG file name as Sample.java, in the same package create one more class as ExecutableRar and write the below code :
public class ExecutableRar {
public static void main(String[] args) {
TestNG testng = new TestNG();
Class[] classes = new Class[]{Sample.class};
testng.setTestClasses(classes);
testng.run();
}
Now you can run this class as a Java Application. Then right click on the package --> Export --> Java --> Runnable jar File --> select ExecutableRar in launch configuration --> Browse the file location and enter the name of the file in Export Destination --> Finish.
Please let me know if you are having any issues.

Eclipse how to reference file in a different src under the same project

My current setup in eclipse is like this:
trunk
--working/src
--resources
I have a java file inside a package under working/src and I am trying to retrieve a file in the resources. The path I am using is "../resources/file.txt". However I am getting an error saying that the file does not exist.
Any help would be appreciated thanks!
Considering your structure
I have package like
trunk
working/src/FileRead.java
resources/name list.txt
Following Code might solve your problem
package working.src;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class FileRead {
public FileRead() {
URL url = getClass().getResource("/resources/name list.txt");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String nameList;
while ((nameList = in.readLine()) != null) {
System.out.println(nameList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FileRead();
}
}
Files will be referenced relative to your project path, so use "resources/file.txt" to reference the file.
However, if you want the file to be accessible when you export the program as a JAR, the path "resources/file.txt" must exist relative to your JAR.
It depends on how you have specified your Java Build Path inside eclipse. I have tested two setups with different results:
Define the directory working/src only as build path. You can get the information what is in your build path through: Select project > Properties > Java Build Path > Tab Source. There are all source folders listed. You see there that there is a default output folder defined. All resources that are contained in a build path are copied to the default output folder, and are then available for the eclipse classes there. You can check that in the resource perspective in your bin folder if the resource is copied there. In this setup, only the classes are generated, resources are not copied (and therefore not found).
Define the directory working/src and resources as build path. Then the resource is copied and may then found by the path "file.txt"
So eclipse has a simple build process included and hides that from the developer, but it is a build process nonetheless.