1

I want to run karate as a stand-alone executable or jar, telling it what feature(s) to run via the command-line.

However, I need to build my own executable or jar, because I need to implement the HttpLogModifier interface in order to apply custom log masking.

The documentation tells me:

Here's what I did:

  1. followed the Quickstart documentation to create a maven archetype project.
  2. deleted the example features and .java files.
  3. created a java package with my implementation of HttpLogModifier.
  4. here is what my directory structure looked like:
$ ls -R .
pom.xml src

./src:
test

./src/test:
java

./src/test/java:
headers          karate-config.js logback-test.xml

./src/test/java/headers:
DemoLogModifier.java
$

I ran mvn clean, compile, test, and install and then tried to run my feature file, using the genarated jar file and giving the path to my feature file:

$ java -jar aca-karate-1.0-SNAPSHOT.jar -m ~/aca/lab/karate/karate-poc/fldfin-view.feature
no main manifest attribute, in aca-karate-1.0-SNAPSHOT.jar
$

but as you can see the jar doesn't have a main method.

I tried another way, but it also did not work:

$ java -cp aca-karate-1.0-SNAPSHOT.jar com.intuit.karate.Main -m ~/aca/lab/karate/karate-poc/fldfin-view.feature
Error: Could not find or load main class com.intuit.karate.Main
$

I'm guessing I need to add a java class file that implements Main, but I have no idea what to name it, where to put it, or what its contents should be.

Thanks for your reading and whatever help you lend.

Jon Detert
  • 51
  • 4

1 Answers1

0

This is beyond the scope of what Karate is designed for, but I think the solution is to do a Maven "shade": https://maven.apache.org/plugins/maven-shade-plugin/

This will create a fat-jar where you can ensure the Karate "main" method (or any one you choose) is in the right place.

Or maybe you don't need to do even that, you can refer to any class within the JAR by NOT using -jar and using -cp instead. You will find an explanation here: https://stackoverflow.com/a/56458094/143475

I think that will get you a solution.

Also refer to this if it has some hints: https://stackoverflow.com/a/58339662/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I am confused - I want to use karate to write and run tests for the API of external projects. I.e. I don't have a project to _add_ karate to. I just want to write tests in karate feature files, and run them with either a stand-alone executable or jar. Are you saying that's beyond the scope of karate's intended design? – Jon Detert Feb 22 '23 at 15:34
  • If that's within scope, then I'm wondering how to do that _and_ do so with custom log masking, like described here: https://github.com/karatelabs/karate#log-masking – Jon Detert Feb 22 '23 at 15:36
  • I.e. I don't have my own custom java to build and run except for the class to implement custom log masking. I'm trying to understand how to run karate with my custom log masking class. – Jon Detert Feb 22 '23 at 15:45
  • @JonDetert okay, when you said "build" I got confused. just ignore my first line. can you focus on the second link in my answer. you do need to compile that one java file you have somehow and add it to the classpath. yes, it will require you to probably work with someone who knows java. I know this is not ideal, but you or your team are most welcome to weigh in here and contribute code: https://github.com/karatelabs/karate/issues/2208 – Peter Thomas Feb 22 '23 at 16:52
  • 1
    Okay, I think I understand what I need to do. I'll try and report back – Jon Detert Feb 22 '23 at 18:33