Error importing jar in groovy script (soapui) - java

I have a problem with running java code from groovy script (groovy script is a part of SoapUI test suite)
i create simple script:
import myjar.jar
new TopClass().sayHello()
the code of TopClass:
public class TopClass {
public void sayHello (){
System.out.println("Hello");
}
}
I put myjar.jar into both soapui-pro-2.5\lib and soapui-pro-2.5\bin\ext folders.
But running script I get:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed, Script1.groovy: 2: unable to resolve class myjar.jar #
line 2, column 1.org.codehaus.groovy.syntax.SyntaxException: unable to
resolve class myjar.jar # line 2, column 1. at
org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:113)
at
org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:970)
at
org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:141)
at
org.codehaus.groovy.control.CompilationUnit$5.call(CompilationUnit.java:527)
at
org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:772)
at
org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:438)
at
groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:281)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:572) at
groovy.lang.GroovyShell.parse(GroovyShell.java:584) at
groovy.lang.GroovyShell.parse(GroovyShell.java:564) at
groovy.lang.GroovyShell.parse(GroovyShell.java:603) at
Please help me to find what I'm doing wrong

Putting the jar under soapui-pro-2.5\bin\ext is all you need for the classes to be found (although restarting SoapUI won't hurt).
However - you should check that the error you get is related to your jar. Is com.my.research available within myjar.jar? If no - just add it.
If yes, add more detailed information to your post.

import myjar.jar
I believe this is not correct, you should be importing the name of the java package not the name of the jar.
Hope this helps

On non-windows implementations of soapui I find it helps if you add it to the .sh file that starts soapui explicitly.

Related

Apache Jena: Error: "java.lang.NoClassDefFoundError: org/apache/jena/riot/RDFDataMgr"

Problem explanation
I have recently been trying to use Apache Jena with Java (rather than on the command line). I wrote a simple script to convert read and write differetn RDF format types, as so
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.query.Dataset;
import org.apache.jena.riot.Lang;
public class Go_NT
{
public static void main(String[] args)
{
Dataset dataset = RDFDataMgr.loadDataset("triail.nq");
RDFDataMgr.write(System.out, dataset, Lang.NTRIPLES);
}
}
triail.nq is a test nquads file containing 81 quads.
I invoked it as so:
javac -cp "/mnt/e/Tráchtas/apache-jena-3.17.0/lib/*" Go_NT.java
java Go_NT
It compiles without error, but when I run it, it returns an error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/jena/riot/RDFDataMgr
at Go_NT.main(Go_NT.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.jena.riot.RDFDataMgr
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
What I have tried
I have looked around and seen that this error occurs almost always because a necessary .jar file is not included, so a class referenced by the code cannot be loaded. The solution to these other issues was to include all of /apache-jena-3.17.0/lib/* . Oddly enough, that has not worked for me--I do include all of the contents of lib/ in my classpath, but I am still seeing the error.
System notes
I am running Jena 3.17.0, using the default Linux binaries available here (https://jena.apache.org/download/index.cgi). I have not added or removed any other Jena modules.
I am running this in the Windows Subsystem for Linux (version 2) with Ubuntu 20.04.
If any of you have any insight into what could be causing this, I would greatly appreciate it!
Based on the comment by vvs, the link https://howtodoinjava.com/java-examples/set-classpath-command-line/ helped out a lot. There were 2 issues: I needed to include the classpath in the java command, not just javac. I also needed to include the current directory where the output of javac would be.
I fixed this by setting the CLASSPATH variable, and then adding all the needed directories to that. (You could also do this by adding the classpath into the -cp argument). Note that the : separates different directories.
In short, here is what I did:
export CLASSPATH=/mnt/e/Tráchtas/apache-jena-3.17.0/lib/*:.
javac Go_NT.java
java Go_NT
Note that you need to re-assign CLASSPATH each time you open a new terminal.

can't run .jar file /java.jar: line 1: public: command not found

I have Ubuntu 16.04.
and downloaded a JDK with a tar.gz file extension and followed This wikihow to install it.
When I try to run a .jar game (like Minecraft) It works successfully, and I have netbeans downloaded that is connected to the same JDK and compiled some programs that i can run in terminal, But When I type :
./Hello_world.jar
Which is :
package main;
public class project {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
I get this output :
./Hello_world.jar: line 1: $'PK\003\004': command not found
./Hello_world.jar: line 2: $'\b.\020oK': command not found
./Hello_world.jar: line 3: syntax error near unexpected token `)'
./Hello_world.jar: line 3:-oK�}����META-INF/MANIFEST.MFM�1
�0��#��uHh Q���X� ��N1�Ҧ$)��7�(�p�ww
�A����|��}�1���ή�n��p<�Рŗ��:CpN~�s�ν�˚�3��%
��)���goPK`
Simple: JAR files aren't executables. You can only invoke binaries/scripts by telling your shell to ./command.
They are archives that contain compiled Java classes.
Thus you use them like:
java -jar somejar.jar
This starts a java virtual machine, and tells it to open the given JAR file. The JVM will then figure the "main" class to run from the meta information that can be backed into the JAR file - to then "run" that main class.
( assuming that the corresponding JAR file has been built in a why that allows running it like this. see here for details on how you enable this "easy way" of running a JAR file )
And just in case: with some scripting magic, you actually can turn a JAR file into a "binary", see here for example.

Command: java -jar [...] Fails with Error Message

I am running java 1.8.0_65 on Windows 7.
I create a JAR and run it with the following command:
java -jar printxml.jar
And get this error:
Error: Could not find or load main class printxml.PrintXml
Here is my command to create the JAR:
jar cmfev manifest.txt printxml.jar printxml.PrintXml #filelist.txt
Contents of file "manifest.txt":
Class-Path: C:\Users\Me\SQLSER~1\JDBC\jtds-1.3.1.jar
I checked whether printxml.PrintXml class is in the JAR via this command:
jar tvf printxml.jar printxml/PrintXml.class
The command succeeded, i.e. PrintXml class is in the JAR.
I then checked if the PrintXml class in the JAR has a "main" method via this command:
javap -classpath printxml.jar -public printxml.PrintXml
The command succeeded and its output included...
public static void main(java.lang.String[]);
Searching the Internet, I found only the obvious answers, like:
Your classpath is wrong.
Your class doesn't have a "main" method.
Can someone please tell me how to resolve this problem?
Thanks,
Avi.
As Homer Simpson would say: D'OH
The value of the Class-Path entry in file "manifest.txt" is wrong!
It needs to be a URL!
So I changed it to:
file:/C:/Users/Me/SQLSER~1/JDBC/jtds-1.3.1.jar
Hey presto! No more error message. Now it runs!
Thanks to all who helped. ;-)

java.lang.NoClassDefFoundError with HBase Scan

I am trying to run a MapReduce job to scan a HBase table. Currently I am using the version 0.94.6 of HBase that comes with Cloudera 4.4. At some point in my program I use Scan(), and I properly import it with:
import org.apache.hadoop.hbase.client.Scan;
It compiles well and I am able to create a jar file too. I do it by passing the hbase classpath as the value for the -cp option. When running the program, I obtain the following message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/client/Scan
I run the code using:
hadoop jar my_program.jar MyJobClass -libjars <list_of_jars>
where list_of_jars contains /opt/cloudera/parcels/CDH/lib/hbase/hbase.jar. Just to double-check, I confirmed that hbase.jar contains Scan. I do it with:
jar tf /opt/cloudera/parcels/CDH/lib/hbase/hbase.jar
And I can see the line:
org/apache/hadoop/hbase/client/Scan.class
in the output. All looks ok to me. I don't understand why is saying that Scan is not defined. I pass the correct jar, and it contains the class.
Any help is appreciated.
Setting the HADOOP_CLASSPATH variable fixed the issue:
export HADOOP_CLASSPATH=`/usr/bin/hbase classpath`

Calling Java from Python: "Can't find or load class" Error

I'm trying to call a java program from python using command line. The code is as follows:
subprocess.check_output(["java", "pitt.search.semanticvectors.CompareTerms", "-queryvectorfile","termvectors.bin","term1","term2"])
I get the following error:
Error: Could not find or load main class pitt.search.semanticvectors.CompareTerms
This happens when I run the program from PyDev (version 2.5 in Eclipse 3.7.2). However, if I run the same code from the terminal, it works and I get the result I want.
I'm almost sure that the problem is related with some configuration of PyDev and how it handles the java CLASSPATH, which is:
/Users/feralvam/Programas/semanticvectors-3.4/semanticvectors-3.4.jar:/Users/feralvam/Programas/lucene-3.5.0/lucene-core-3.5.0.jar:/Users/feralvam/Programas/lucene-3.5.0/contrib/demo/lucene-demo-3.5.0.jar:
The class "pitt.search.semanticvectors.CompareTerms" is in "semanticvectors-3.4.jar".
Any help you could give me would be really appreciated.
Thanks!
The solution proposed by #eis worked. Now, the command is:
subprocess.check_output(["java", "-classpath", "/Users/feralvam/Programas/semanticvectors-3.4/semanticvectors-3.4.jar:/Users/feralvam/Programas/lucene-3.5.0/lucene-core-3.5.0.jar:/Users/feralvam/Programas/lucene-3.5.0/contrib/demo/lucene-demo-3.5.0.jar:", "pitt.search.semanticvectors.CompareTerms", "-queryvectorfile","/Users/feralvam/termvectors.bin","term1","term2"])

Categories

Resources