how to create executable jar file with cucumber tests? - java

My service create an executable jar by gradle. When i create and run my jar (java -jar file.jar) i recive error:
no main manifest attribute, in "file.jar"
because i don't have main_class.
I created main method:
public static void main(final String[] args) throws Throwable {
String[] arguments = {"--plugin", "html:build/reports/cucumber", "src/test/resources/features", "--glue", "src/test/java/steps"};
cucumber.api.cli.Main.main(arguments);
}
and My program founds the features but doesn't found glue code.
Could someone help me with this problem? Thank you in advance.

The value for the glue option should be a java classpath. And the feature files should be the last option.
{"--plugin", "html:build/reports/cucumber", "--glue", "steps", "src/test/resources/features"}

The below code worked for me to execute the cucumber tests from runnable jar with test frame work as TestNG.
Executing jar:
java -jar ProductsAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar
import io.cucumber.core.cli.Main;
public static void main(String args[]) throws Throwable {
try {
Main.main(new String[] {
"-g","com.sadakar.cucumber.common",
"-g","com.sadakar.cucumber.runner",
"classpath:features",
"-t","#SmokeTest",
"-p", "pretty",
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",
"-m"
}
);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Main method exception : " + e);
}
}

Related

How to add testng parameters in a java main method?

I wanted to trigger the execution from a java main method instead of testng.xml file.
My doubt is how to add the parameters to Java main method for the execution. I have found .addListener and .setGroups to add listener and groups respectively, but couldn't able to find a way to add parameters.
Please help me out to start the exection through java main method.
Sample:
public class Execution {
public static void main(String[] args) throws IOException {
TestNG test = new TestNG();
test.setTestClasses(new Class[] {AETVTests.class});
test.addListener(new MyTestListenerAdapter());
test.setGroups("");
test.run();
}
}
If you would reconsider using xml - you can also trigger execution through the main method with the xml file. Add the testng.xml file to your project path (in eclipse you can right click project - new - file - testng.xml), and this will work:
public static void main(String[] args) throws IOException
{
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("C:\\eclipse-2018\\Tests\\testng.xml"); //path to xml
testng.setTestSuites(suites);
testng.run(); //run TestNG
}
You can access args by arg[0],arg[1] likewise. in cmd run your jar file>
java -jar classname.jar param1 param2

How to get path to class file in eclipse and linux in java?

In eclipse for windows, when I run
public class HelloWorld {
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
}
}
It gives me the path of the project root folder (which contains the bin folder which has the class file). For example
SampleProject
and the class file is actually located at
SampleProject\bin\myclass.class
But if I run the same program in linux with
javac myclass.java
java myclass
it gives me the directory that has the .class file, which is the same as pwd command. This is what I want in eclipse for windows. I want some code that will give me the path to the class file in both eclipse for windows and linux.
Does anyone know how do this?
Thanks
If I understand you correctly, you'd like a method that retrieves a class' path on disk. This is easily achievable, like so:
public String getClassPath(Class c) {
try {
return c.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
NOTE this will work even if the class is contained in a jar file. It will return the path to the jar in this case.
The easiest way is to do this:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(HelloWorld.class.getResource("HelloWorld.class"));
}
}

Load a jar in Java Class

I have a simple java class I want to load a jar file in my main method. How can I load the jar so that its available
public static void main(String args[]) throws SQLException, IOException{
String connectionString = "connectionstring";
File platformDB = new File(tempDir, "PlatformDB.eap");
createNewTempDirectory();
Mirror m = new Mirror(connectionString, createEmptyEap(emptyEapName) ,platformDB.getAbsolutePath());
m.run();
}
Mirror Class
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
}
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
}
}
In Mirror.run method I try to make a connection to database
Mirror.run() method
public void run() {
this.source = DriverManager.getConnection(EaDbStringParser.eaDbStringToJdbc(sourceString));
this.source.setReadOnly(true);
}
I have a jar file located under "C:\sqljdbc_4.0\enu\sqljdbc4.jar"
How can I load this jar so the connection is made successfully
Thanks
You just have to run the class with the -classpath option:
java -classpath C:\sqljdbc_4.0\enu\sqljdbc4.jar
The simplest way is to add the jar to the classpath loading the main class. For example if Mirror is the class with the main method where you say from console
`java Mirror'
modify it to
'java -cp C:/sqljdbc_4.0/enu/sqljdbc4.jar Mirror`

Run Non GUI Jar Files

I know that I can run non GUI jar files from the command line. Is there any way that can do so by clicking or something and not writing the commands again and again.? Is there any software to do so. ( I am talking about a compiled jar and don't want to run from any ide)
public static final String TITLE = "CONSOLE title";
public static final String FILENAME = "myjar.jar";
public static void main(String[] args) throws InterruptedException, IOException {
if(args.length==0 || !args[args.length-1].equals("terminal")) {
String[] command;
if(System.getProperty("os.name").toLowerCase().contains("win")) {
command = new String[]{"cmd", "/c", "start \"title \\\""+TITLE+"\\\" & java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
} else {
command =new String[]{"sh", "-c", "gnome-terminal -t \""+TITLE+"\" -x sh -c \"java -jar \\\""+new File(FILENAME).getAbsolutePath()+"\\\" terminal\""};
}
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch(Throwable t){
t.printStackTrace();
}
} else {
//THERE IS YOUR CONSOLE PROGRAM:
System.out.println("Hey! What's your name?");
String read = new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("Hey, "+read+"!");
Thread.sleep(10000);
}
}
You can run it with double clicking on .jar file. Don't forget about MANIFEST.MF! :) (working on linux, also!)
Example (I only double clicked on jar file):
The way intended by Java is that you call java -jar XXXX.jar on the jars you need. Drawback is that you can't specify a classpath so all classes should be there.
A cooler way to package an application is by using Java WebStart. With that the user installs the application jut by clicking on a web browser. Check here http://docs.oracle.com/javase/8/docs/technotes/guides/javaws/developersguide/contents.html

Running java program from another java program

I'm trying to run a Java program from another Java application. Here is my code:
public class Main {
public static int Exec() throws IOException {
Process p = Runtime.getRuntime().exec("javac -d C:/Users/Dinara/Desktop/D/bin "
+ "C:/Users/Dinara/Desktop/D/src/test.java");
Process p1 = Runtime.getRuntime().exec("java -classpath C:/Users/Dinara/Desktop/D/bin test");
return 0;
}
public static void main(String[] args) throws IOException {
Exec();
}
}
javac works fine and creates test.class file in bin directory. However java -classpath C:/Users/Dinara/Desktop/D/bin test does not run the test.class file.
the content of the test.java:
import java.io.*;
class test {
public static void main(String args[]) {
try {
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
out.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
I suppose that something wrong with recognizing Java command. Could you please give me a sample code for fixing this problem or share idea? I'm using Netbeans to run Main class and the location of the application folder is C:\Users\Dinara\Main
Use
System.getProperty("java.home") + "/bin/java -classpath C:/Users/Dinara/Desktop/D/bin test"
instead of
"java -classpath C:/Users/Dinara/Desktop/D/bin test"
You need to supply the full path to the javac, exec won't use the ath to find it for you

Categories

Resources