I have a feature that execute a function from a Java class. So for this reason I use this command to get the class and create a new instance:
When I run this feature with maven (mvn test -Dtest...) everything is okay. The problem is when I run this feature with karate standalone jar, karate can't find the RCNUtils class.
Error:
org.graalvm.polyglot.PolyglotException: TypeError: Access to host class utilities.RCNUtils is not allowed or does not exist.
GraalJS error: https://github.com/oracle/graaljs/blob/master/docs/user/FAQ.md#typeerror-access-to-host-class-commyexamplemyclass-is-not-allowed-or-does-not-exist
I think it's a classpath problem but I have tried with a lot of differents paths and commands to execute the jar, and nothing works.
I don't know if this is a known issue or if there is a karate example using utilities classes and executed with karate.jar
I tested this problem with differents Karate versions. Actually I'm using Karate 1.1.0
Some of the options I have tried with no results:
Use -w / --workdir param to change working directory with no results
Use java -cp instead of java -jar to set classpath following:
Unable to use read('classpath:') when running tests with standalone karate.jar
Use -Dkarate.config.dir param
Note: I don't think it's a security problem because if I try to get "RCNUtils.java" file with "karate.read()" or "read()" in the same feature, it works. I think because I can put the path to the file. The problem is that I can't put the path to java class in "Java.type()" method
Same error here: Executing Karate jar with mock using external library Spring Framework
Thanks in advance.
Related
I need to tune the JVM memory by help of Gradle. I've writen settings in the gradle.properties file:
org.gradle.jvmargs=-XX:MaxMetaspaceSize=60M
but it didn't work. Then I've done it manually in an IDEA (Run>Edit Configurations...) and wrote the settings for the needed class (in VM options: field) and it works. Can we do the same by help of Gradle?
I've tried to do:
org.gradle.jvmargs=-XX:MaxMetaspaceSize=60M test1.Test
but I got the error:
Error: Could not find or load main class test1.Test
When you run your application via IDEa, it runs the application using java ... command and gradle is not involed at all, so whatever you put in build.gradle doesn't matter.
If you want to run your app via gradle, consider using application plugin, which allows you to set jvm args. If you use Spring Boot use can use bootRun task and specify params in jvmArgs property.
I would like to run the main method of a java class by using #Grab so that requirements are taken care of automatically. More specifically I would like to run the pdfbox example https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/RemoveAllText.java
I wrote the following groovy script
#!/usr/bin/env groovy
#Grab('org.apache.pdfbox:pdfbox-examples:2.0.20')
import org.apache.pdfbox.examples.util.RemoveAllText
RemoveAllText.main(args)
The #Grab, import and execution of main seems to work. But the main seems to recall itself repeatedly thus failing with a StackOverflowError as below.
Caught: java.lang.StackOverflowError
java.lang.StackOverflowError
at RemoveAllText.main(RemoveAllText.groovy)
at RemoveAllText$main.call(Unknown Source)
at RemoveAllText.run(RemoveAllText.groovy:5)
at RemoveAllText.main(RemoveAllText.groovy)
...
I am new to groovy so I am not sure what I am doing wrong. Is what I am trying to do possible? If it is possible, how would it be done?
To make the example fully reproducible I get the above error when I use the pdf found at https://github.com/mozilla/pdf.js/raw/v2.4.456/examples/learning/helloworld.pdf and using groovy version 2.4.16 installed using the default repositories in Ubuntu 18.04. The command run would be
groovy RemoveAllText.groovy helloworld.pdf helloworld_out.pdf
If I manually download the required jar files and I run
java -cp pdfbox-2.0.20.jar:commons-logging-1.2.jar:pdfbox-examples-2.0.20.jar org.apache.pdfbox.examples.util.RemoveAllText helloworld.pdf helloworld_out.pdf
it works without problem.
Rename your script from RemoveAllText.groovy to something else and everything should be fine.
Problem that your groovy script produces the same class name as Apache class.
I use eclipse for my optaplanner project development and I am able to execute my code from eclipse without any problem. To make this project as an executable, I exported it as a jar file using eclipse export wizard.
Then I run my jar from terminal using the command java -jar myOptaPlanner.jar, then, I get below error
WARN given scan urls are empty. set urls in the configuration
Exception in thread "main" java.lang.IllegalStateException: The scanAnnotatedClasses
(ScanAnnotatedClassesConfig()) did not find any classes with a PlanningSolution
annotation.
Maybe you forgot to annotate a class with a PlanningSolution annotation.
Maybe you're using special classloading mechanisms (OSGi, ...) and this is a bug.
If you can confirm that, report it to our issue tracker and workaround it by
defining the classes explicitly in the solver configuration.
at org.optaplanner.core.config.domain.ScanAnnotatedClassesConfig.loadSolutionClass(ScanAnnotatedClassesConfig.java:106)
at org.optaplanner.core.config.domain.ScanAnnotatedClassesConfig.buildSolutionDescriptor(ScanAnnotatedClassesConfig.java:86)
at org.optaplanner.core.config.solver.SolverConfig.buildSolutionDescriptor(SolverConfig.java:270)
at org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:216)
at org.optaplanner.core.impl.solver.AbstractSolverFactory.buildSolver(AbstractSolverFactory.java:57)
at org.roster.app.WorkerRosteringApp.main(WorkerRosteringApp.java:36)
Is this really a bug or am I exporting it wrong?
Update: I tried to export jar using maven-shade-plugin and here also I am getting the same error.
Thanks
scanAnnotatedClasses doesn't seem to work in a jar file. Instead, use following workaround:
<solutionClass>path.to.your.SolutionClass</solutionClass>
<entityClass>path.to.your.EntityClassI</entityClass>
<entityClass>path.to.your.EntityClassII</entityClass>
and delete the scanAnnotatedClasses in your config file.
If you don't want to declare the solution and entity classes manually, including the packages containing the solution and entity classes also works as workaround:
<scanAnnotatedClasses>
<packageInclude>your.package.here</packageInclude>
</scanAnnotatedClasses>
Documentation about <packageInclude> can be found here
I'm developing a C# application to run JUnit tests automatically. Since it's complicated to move the current location where my C# application is running, i need to execute the JUnit tests without directly go inside their folder.
So, i have this folder tree:
» ProjectFolder
»» ClassesFolder
»» TestsFolder
»»» testX.java
»»» testX.class
I'm already able to compile the files, using:
javac ...\ProjectFolder\ClassesFolder\*.java
javac ...\ProjectFolder\TestsFolder\*.java
But i can't execute the tests. I tried:
java -classpath ...\ProjectFolder org.junit.runner.JUnitCore TestsFolder.testX
And got this error:
Error: Could not find or load main class org.junit.runner.JUnitCore
I even try this way:
java org.junit.runner.JUnitCore ...\ProjectFolder TestsFolder.testX
But, once again, it doesn't work:
JUnit version 4.10
Could not find class: -classpath
Could not find class: ...\ProjectFolder
Could not find class: TestsFolder.testX
Time: 0,003
OK (0 tests)
So, my main doubt, is how i can use the options (like -cp or -classpath) of the java command, when executing JUnit tests using org.junit.runner.JUnitCore.
Thanks in advance.
The problem is that the jUnit-runner is not in the classpath you specify. You need to add the path to junit.jar to the classpath.
Google is nice http://www.jsystemtest.org/?q=node/44
But why not just use something that already exists and can take care of this? Like Maven or Ant?
Greetings,
I'm playing around with mahout, I've written a basic java class which imports some of the libraries. It seems my classpath is correct when compiling, I get no errors or complaints at all.
However when I run the compiled class I get an exception saying...
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
My guess is that . is not on your classpath. For example, you might be compiling with:
javac -cp foo.jar:bar.jar Test.java
but then to run the code you'd need
java -cp foo.jar:bar.jar:. Test
The code that you're compiling doesn't need to be on the classpath as you're providing the code (so there's nothing to find) - that's why it manages to compile but not run.
That's only a guess, of course - if you could post the commands you're using to compile and run the code, that would help.
I'm now getting an error saying java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
You're missing slf4j-api.jar on your class path. With SLF4J, you always need slf4j-api.jar and another jar to bind a logging framework. And actually, if you don't care about logging, use slf4j-nop.jar instead of slf4j-log12.jar.
Update: Mahout seems to be available in Maven central repository so using Maven could ease the class path setup process. And if you're not into learning Maven, consider using MOP which is a command line launcher to run Java stuff that can transparently download Maven artifacts and their dependencies and setup your classpath.
Compile time classpath sounds right; runtime classpath is wrong.
From the javadocs for that class:
Thrown if the Java Virtual Machine or
a ClassLoader instance tries to load
in the definition of a class (as part
of a normal method call or as part of
creating a new instance using the new
expression) and no definition of the
class could be found.
The searched-for class definition
existed when the currently executing
class was compiled, but the definition
can no longer be found.
Do you see a Test.class file in the current directory? Maybe you compiled it to another path by mistake.
If you are using Mahout, be aware that after you build it with Maven, it will generate "*.job" files in the target/ directory, which contain all dependencies packaged together. It is just a .jar file.