I am trying a simple json java program (no maven or IDE).
This is my java file:
import javax.json.*;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonWriter;
class JsonTest {
public static void main(String[] args){
JsonObject obj = Json.createObjectBuilder()
.add("name", "foo")
.add("num", new Integer(100))
.add("balance", new Double(1000.21))
.add("is_vip", new Boolean(true)).build();
System.out.print(obj);
}
}
In the same directory of this file, I have the jar file:
javax.json-1.0.jar
I don't get any error in compiling:
javac -classpath ./javax.json-1.0.jar JsonTest.java
But when I run the program I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/json/Json
at JsonTest.main(JsonTest.java:10)
Caused by: java.lang.ClassNotFoundException: javax.json.Json
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Why am I getting this error when there was no error in compilation. I already searched other questions with this error but unlike them I am not using any IDE or maven. Any help will be appreciated.
You are not running it with the JSON JAR file on the classpath.
Try this:
java -classpath .:./javax.json-1.0.jar JsonTest
Change : to ; on Windows ....
Related
I am trying to inject a .jar file in a running VM.
I've added tools.jar in the build path in eclipse, but when I try to run the injector, this error pops up. How should I add tools.jar to the project?
Full error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/attach/VirtualMachine
at src.testinjector.MainClass.main(MainClass.java:13)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.attach.VirtualMachine
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
you may try:
Give the path to tools.jar instead of being dependent on JAVA_HOME.
Note: JAVA_HOME/path to jdk/bin/ should not have any space.
By default, the "tools.jar" classes are not loaded, your options are to either use a jdk that does load it, include tools.jar in your jar, or forcefully load it with the method below, (note: this might not work on certain jdk/jvm's)
private static void prepareAttach() throws NoSuchMethodException, MalformedURLException, InvocationTargetException, IllegalAccessException {
String binPath = System.getProperty("sun.boot.library.path");
// remove jre/bin, replace with lib
String libPath = binPath.substring(0, binPath.length() - 7) + "lib";
URLClassLoader loader = (URLClassLoader) [This Class].class.getClassLoader();
Method addURLMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
addURLMethod.setAccessible(true);
File toolsJar = new File(libPath + "/tools.jar");
if (!toolsJar.exists()) throw new RuntimeException(toolsJar.getAbsolutePath() + " does not exist");
addURLMethod.invoke(loader, new File(libPath + "/tools.jar").toURI().toURL());
}
Peace be upon you,
Based on this link, I followed the required steps for setting up the JavaSci in Windows; but I encounter this problem
c:\Backups>javac -cp "C:\Program Files\scilab-5.5.0\modules\javasci\jar\org.scilab.modules.javasci.jar;C:\Program Files\scilab-5.5.0\modules\types\jar\org.scilab.modules.types.jar";. TestSciLab.java
c:\Backups>set PATH=%PATH%;"C:\Program Files\scilab-5.5.0\bin"
c:\Backups>java -cp "C:\Program Files\scilab-5.5.0\modules\javasci\jar\org.scilab.modules.javasci.jar;C:\Program Files\scilab-5.5.0\modules\types\jar\org.scilab.modules.types.jar";. TestSciLab
The native library javasci does not exist or cannot be found.
java.lang.UnsatisfiedLinkError: no javasci in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at org.scilab.modules.javasci.Call_ScilabJNI.<clinit>(Unknown Source)
at org.scilab.modules.javasci.Call_Scilab.SetFromJavaToON(Unknown Source)
at org.scilab.modules.javasci.Scilab.initScilab(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at TestSciLab.main(TestSciLab.java:7)
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.scilab.modules.javasci.Call_ScilabJNI.SetFromJavaToON()V
at org.scilab.modules.javasci.Call_ScilabJNI.SetFromJavaToON(Native Method)
at org.scilab.modules.javasci.Call_Scilab.SetFromJavaToON(Unknown Source)
at org.scilab.modules.javasci.Scilab.initScilab(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at org.scilab.modules.javasci.Scilab.<init>(Unknown Source)
at TestSciLab.main(TestSciLab.java:7)
c:\Backups>
The only difference that you see in the above screen and the guides in the link is the place of %path% that I moved it to the beginning. But do not doubt! even I checked it with the reverse order and I got the error.
My code is as simple as
import org.scilab.modules.javasci.JavasciException.InitializationException;
import org.scilab.modules.javasci.Scilab;
public class TestSciLab {
public static void main(String[] args) throws InitializationException {
Scilab sci = new Scilab();
}
}
Any lighting up points?
On Windows, you just need to add Scilab's bin folder to %PATH%.
In C:\ru\compscicenter\java\stacks> I have placed
AbstractStack.java
ArrayStack.java
LinkedStack.java
Stack.java
StackTest.java
The first string of each of these files is
package ru.compscicenter.java.stacks;
My my CLASSPATH examination:
C:\ru\compscicenter\java\stacks>echo %CLASSPATH%
.;C:\ru\compscicenter\java\stacks
When I'm in the stacks directory and try to compile StackTest, I fail to do that.
What I write and what I get is here:
C:\ru\compscicenter\java\stacks>javac StackTest.java
StackTest.java:4: error: cannot find symbol
public static void fill(Stack<String> stack){
^
symbol: class Stack
location: class StackTest
StackTest.java:12: error: cannot find symbol
public static <E> void dump(Stack<E> stack){
^
symbol: class Stack
location: class StackTest
StackTest.java:24: error: cannot find symbol
LinkedStack<String> stack = new LinkedStack<String>();
^
symbol: class LinkedStack
location: class StackTest
StackTest.java:24: error: cannot find symbol
LinkedStack<String> stack = new LinkedStack<String>();
^
symbol: class LinkedStack
location: class StackTest
StackTest.java:29: error: cannot find symbol
ArrayStack<String> stack = new ArrayStack<String>(10);
^
symbol: class ArrayStack
location: class StackTest
StackTest.java:29: error: cannot find symbol
ArrayStack<String> stack = new ArrayStack<String>(10);
^
symbol: class ArrayStack
location: class StackTest
6 errors
Could you help me correct this?
Added later:
Then I did this:
C:\>javac c:\ru\compscicenter\java\stacks\*.java
Note: c:\ru\compscicenter\java\stacks\ArrayStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\>java StackTest
Exception in thread "main" java.lang.NoClassDefFoundError: StackTest (wrong name: ru/compscicenter/java/stacks/StackTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Could you help me again?
While compling use something like this
Javac -d . *.java
it will automatically create package structure and place class files in corresponding packages
The packages are interpreted as relative to the current directory. If you are specifying
package ru.compscicenter.java.stacks;
then the .java and .class files need to be in the directory ru\compscicenter\java\stacks\ from wherever you're running the commands. In this case, you need to run javac and java from C:\.
Start by defining a root directory for your app. Placing everything directly in c:\ is really not a good idea:
c:\myapp
then create a directory for the sources (.java files):
c:\myapp\src
and another one for your classes (.class files):
c:\myapp\classes
Now, your source tree should match exactly with your package tree, so you should have your sources files under
c:\myapp\src\ru\compscicenter\java\stacks
since they're all in the package ru.compscicenter.java.stacks.
Place yourself at the root of your app, in c:\myapp. The javac compiler expects files as arguments. And you want to place the compiled .class files in c:\myapp\classes. So you want to use
javac -d classes src\ru\compscicenter\java\stacks\*.java
This will create a folder tree matching exactly with the package tree in c:\myapp\classes, containing the compiled .class files. The root of the tree is c:\myapp\classes, so that's what you need to add to the classpath to run the app. And java expects a fully qualified class name:
java -cp c:\myapp\classes ru.compscicenter.java.stacks.StackTest
or, using a relative path, since you're in c:\myapp already:
java -cp classes ru.compscicenter.java.stacks.StackTest
I am trying to call java class from Groovy script in SoapUI
My Java Class
public class LogTest {
public void sayHello (){
System.out.println("Hello");
}
}
Groovy script
import LogTest;
new LogTest().sayHello()
I places the LogTest.class in ...SmartBear\SoapUI-4.6.1\bin\ext path and restart SoapUI
But still I am getting following error in SoapUI
:ERROR:org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script5.groovy: 1: unable to resolve class LogTest
# line 1, column 1.
import LogTest;
^
org.codehaus.groovy.syntax.SyntaxException: unable to resolve class LogTest
# line 1, column 1.
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:148)
at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1240)
at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:148)
at org.codehaus.groovy.control.CompilationUnit$8.call(CompilationUnit.java:601)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:839)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:544)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:493)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:731)
at groovy.lang.GroovyShell.parse(GroovyShell.java:743)
at groovy.lang.GroovyShell.parse(GroovyShell.java:770)
at groovy.lang.GroovyShell.parse(GroovyShell.java:761)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:148)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:149)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.runTestStep(AbstractTestCaseRunner.java:239)
at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runCurrentTestStep(WsdlTestCaseRunner.java:48)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:148)
at com.eviware.soapui.impl.wsdl.support.AbstractTestCaseRunner.internalRun(AbstractTestCaseRunner.java:43)
at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
What could be the possible root cause
Cheers
Were you able to resolve this issue? If not I ran into the same scenario, and here is how I fixed it.
I had a jar file (HelloWorldTest.jar) with the following information:
package helloworldtest;
public class HWT {
public static String Testing() {
return "Hello World!";
}
}
Notice that my file name and package name are different. Make sure when importing into Groovy you use the package name.
I used:
import helloworldtest.HWT;
log.info HWT.Testing();
Result would be:
Thu Mar 06 16:26:01 PST 2014:INFO:Hello World!
Build a jar file containing your code (LogTest)
Put that jar file in the /bin/ext directory.
This will allow you to call your code from a Groovy script.
Following program runs fine with Netbeans IDE but when i try to run from command prompt i get
Exception in thread "main" java.lang.NoClassDefFoundError: Gcd (wrong name: algo
rithms/Gcd)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
package algorithms;
public class Gcd {
public static int ComputeGcd(int number1, int number2){
if(number2 == 0){ return number1;}
else{
int remainder = number1 % number2;
return ComputeGcd(number2,remainder);
}
}
public static void main(String[] args) {
int a = 32;
int b = 12;
System.out.println(ComputeGcd(a,b));
}
}
You should be in the src directory, running these commands:
...\src> javac algorithms\Gcd.java
...\src> java algorithms.Gcd
(You don't have to compile from that directory, but I would suggest you do so.)
The java command takes the fully-qualified class name, which includes the package name.
move two levels above i.e. src and then do a java algorithms.Gcd
Have you created directory for algorithm, try running the program by commenting package algorithm, it will work...Once assured, you can use
javac algorithms.Gcd.java;
java algorithms.Gcd`
as mentioned by #JonSkeet, you can learn more about the package structures here,
You probably need to see what classpaths Netbeans is providing to Java that your CLI command isn't.
Try removing the package call if you're not using packages, as it can lead to complications.