When I run the following code, it throws an Exception, why? - java

code:
public class JniTest {
static {
System.load("/usr/lib/libJniTest.so"); //It run have no exception and true
//System.loadLibrary("libJniTest");
}
public native void sayHello();
public static void main(String[] args) {
System.out.println(System.getProperties().getProperty("java.library.path"));
new JniTest().sayHello();
}
}
If use the comments code instead of "System.load("/usr/lib/libJniTest.so"), it will throw exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no libJniTest in java.library.path!
I have input
export LD_LIBRARY_PATH=/usr/lib in /etc/profile and set vm arguments:
-Djava.library.path=/usr/lib
Help me, Please!

Set VM arguments with :
-Djava.library.path="${workspace_loc}/project_name:${env_var:PATH}"
and make sure that .so should be in your project folder

Related

How to resolve ClassNotFoundException when the class is there?

Here is my code
package vista;
public class MyMainClass
{
public static void main(String[] args)
{
try
{
if(1>0) throw new MyException("ERROR");
}
catch(MyException err)
{
System.out.println(err.toString());
}
}
}
package vista;
public class MyException extends Exception
{
// Constructor.
public MyException(String errMsg)
{
super(errMsg);
}
}
Output:
Error: Unable to initialize main class vista.MyMainClass
Caused by: java.lang.NoClassDefFoundError: vista/MyException
Command execution failed.
Both classes are in the same \vista folder, and before excecution NetBeans recognizes this. How do I resolve this?
Edit: while not running but just compiling the program I realized NetBeans was attempting to download some files. I turned my firewall off, ran the program, it downloaded some files, and now it excecutes properly.

VS Code still runs Java file even when build fails?

Simple question here. Here is my Java file:
public class Test {
public static void main(String []args) {
System.out.println("It ran!");
}
void a() {
qweifjew;
}
}
When I press "Run" on VS Code, it says build failed do you want to continue? Makes sense since I have compile-time errors. But when I press continue, it is still able to run and display "It ran!". How come?
For more information on the run command:
C:\Users\jeffe\coding-tutorials\learning-jest> cd c:\Users\jeffe\coding-tutorials\learning-jest && c:\Users\jeffe\.vscode\extensions\vscjava.vscode-java-debug-0.27.1\scripts\launcher.bat "C:\Program Files\Java\jdk-11.0.2\bin\java.exe" -Dfile.encoding=UTF-8 -cp C:\Users\jeffe\AppData\Roaming\Code\User\workspaceStorage\5e0a770d0910238b624ead6f98bca1ec\redhat.java\jdt_ws\learning-jest_f8aabfb2\bin Test
It ran!
This is the decompiled .class file of your code:
public class Test {
public Test() {
}
public static void main(String[] args) {
System.out.println("It ran!ddfseffe");
}
void a() {
throw new Error("Unresolved compilation problems: \n\tSyntax error, insert \"VariableDeclarators\" to complete LocalVariableDeclaration\n\tqweifjew cannot be resolved\n");
}
}
You have Auto Save ON in VS code ?
It's able to run a previous successful build to give you an output.

How to initialize MPJ Express Outside main method?

I'm trying to initialize the MPJ inside a function but I cant make the MPI.Init(args) work. I tried dragging along the args like this
import mpi.*
public class Test {
String [] args
public static void main(String [] args){
Test.args = args;
t.testFunction();
}
public void testFunction() {
MPI.Init(args);
}
}
but I get an exception like this
Exception in thread "AWT-EventQueue-0" mpi.MPIException: Usage: java
MPI conf_file can be,
../conf/xdev.conf OR
http://holly.dsg.port.ac.uk:15000/xdev.conf
at mpi.MPI.Init(MPI.java:232)
The problem is my program works with an action listener on timer interrupts so I cant put it all inside the main method.
Thanks in advance for any solutions or even ideas.

JNI Could not find or load main class- Unique issue

I tried lot of options to fix this , but could not find a solution. I have created the header file and the dll too. Set the class path asd well. Javac command works fine. When I run this file, I get error: Could not find or load main class com.log.jni.example.HelloWorld. Could you please help me. Here is my file.
public class HelloWorld {
private native void print(String path);
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path="C:\\Capture.pcap";
new HelloWorld().print(path);
}
static {
System.loadLibrary("HelloWorld");
}
}
Could it be that your static initializer is failing.
The following code:
public class Main
{
static
{
if (true)
throw new Error("Error is here");
}
public static void main(String... args)
{
System.out.println("I am running");
}
}
produces the output:
Exception in thread "main" java.lang.Error: Error is here
at Main.<clinit>(Main.java:22)
Could not find the main class: Main. Program will exit.
Are there any stack traces printed out before the 'Could not find main class' error? In this example, the class was found but failed to initialize because of the exception thrown in the static initializer. In your code, a likely suspect is that the System.loadLibrary() call fails with an UnsatisfiedLinkError.
The error "Could not find or load main class ..." occurs when the binary file is not built. Click on project, turn off automatic build. Then click on project and build all. Then turn on automatic build.

Where's the exception message?

I meet a weired problem when I use maven. I execute the following code using "maven exec:java".
Obviously, it should throw a RuntimeException, but I did not see anything in console. But if I execute it in eclipse, I can see the error message. So where does the exception gone ? Thanks
public class HelloWorld {
public static class MyThread extends Thread {
#Override
public void run() {
String str = null;
str = str.trim();
}
}
public static void main(String[] args) throws InterruptedException, IOException {
MyThread thread = new MyThread();
thread.start();
System.in.read();
}
}
Might be a bug of the Maven Exec Plugin (see issues like MEXEC-89 or MEXEC-80). Try with the version 1.2 of the plugin:
mvn org.codehaus.mojo:exec-maven-plugin:1.2:java -Dexec.mainClass="com.acme.Foo"
You could also try adding the -e command line option to your mvn call, I think that solved a similar problem I had.

Categories