Netutils in java - Exception occurs [duplicate] - java

This question already has answers here:
What is LD_LIBRARY_PATH and how to use it?
(6 answers)
Closed 8 years ago.
I am using Netutil class in my simple java app to show all network interfaces, the program code is as follow and the exception result after that. I am absolute beginer and your help is big time saver for me :)
package packet_experience;
import edu.huji.cs.netutils.NetUtilsException;
import edu.huji.cs.netutils.capture.*;
import java.io.IOException;
/**
*
* #author Pete
*/
public class Packet_Experience {
/**
* #param args the command line arguments
* #throws edu.huji.cs.netutils.NetUtilsException
* #throws java.io.IOException
*/
public static void main(String[] args) throws NetUtilsException, IOException {
JPCapInterface intArr[] = JPCap.getAllIntefacesNames();
for (JPCapInterface next : intArr)
{
System.out.println(next.toString());
}
}
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: edu.huji.cs.netutils.capture.impl.JPCapWraper.get_all_devices(Z)Ljava/lang/String;
at edu.huji.cs.netutils.capture.impl.JPCapWraper.get_all_devices(Native Method)
at edu.huji.cs.netutils.capture.impl.JPCapWraper.getAllInterfaces(JPCapWraper.java:299)
at edu.huji.cs.netutils.capture.JPCap.getAllIntefacesNames(JPCap.java:346)
at packet_experience.Packet_Experience.main(Packet_Experience.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Update (Solution!):
My CPU is CORE i5 and having Win 7 Home Pre 64bit.
I tried JVM 32 and 64bit and changed System_PATH and java.library.path and copied all jar, dll, lib,... files there as well. Also I followed and performed recommendation I received on this page and I read in any other place ( as far as I could understand :) ).
At end I encountered with jnetpcap which has similar performance and I recommend it instead as works perfectly for me. You can find it at jnetpcap Homepage . Note that the same as Netutils you should adjust System_PATH and java.library.path and make sure you use JVM 32bit, otherwise you will definitely receive UnsatisfiedLinkError.
Have fun!

The error looks exactly like "You need a dll/so".
Try looking here:
NetUtils java library - how to make it work?
You will find the .dll somewhere on Your computer, just search Your windows dir.

Related

" could not find or load main class " cmd, windows 10, jdk1.8.0_11 [duplicate]

This question already has answers here:
How do I run a Java program from the command line on Windows?
(13 answers)
What does "Could not find or load main class" mean?
(61 answers)
Closed 6 years ago.
import java.util.*;
class sample
{
public static void main(String arg[])
{
System.out.println("Here it is!");
}
}
How to resolve this Error? I tried set path="..." and set classpath="..." But nothing helped me. Can you help me?
mabye you can point the system of where the jdk is. to do this on windows 10, search environment variables in the search bar and click on edit the system environment variables. when you open it, click on environment variables.. at the bottom. you should see 2 sections. in the lower section click new. for the name type JAVA_HOME. it is necessary to have caps but dont include the dot. and for the path, put the directory of the java jdk. always put the folder of the latest version.
This happens because filename of the java file is not same as your class name.
In your case file name must be "sample.java".
If you have multiple classes in this file, file name must contains the class that holds the main method.
Make sure you use capital/simple letters correctly.

UnsatisfiedLinkError: no opencv_java249 in java.library.path

Running into some problems making a piece of code run on my mac.
Had someone write me an image analysis java app but I keep getting this error when trying to run it on netbeans.
run: Exception in thread "main" java.lang.UnsatisfiedLinkError: no
opencv_java249 in java.library.path at
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857) at
java.lang.Runtime.loadLibrary0(Runtime.java:870) at
java.lang.System.loadLibrary(System.java:1119) at
image.prossing.Test.main(Test.java:28) Java Result: 1 BUILD SUCCESSFUL
(total time: 0 seconds)
Have the netbeans project, and added the necessary jar files as libraries. The programmer told me to download the correct OpenCV version and copy the opencv.dll file to my java/jre/bin folder. But I cannot find the dll file or the java/jre folder.
I know most programming happens on windows for a reason. Hope someone can help me resolve this issue and run this application on my mac.
Here is the first part of the code, the part that is most probably creating the error:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package image.prossing;
/**
*
* #author Dumith Salinda
*/
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import static org.opencv.core.Core.FONT_HERSHEY_SIMPLEX;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
public class Test {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Sorry if it's not that clear, let me know what info to add if something is missing or not clear.
Would truly appreciate any help you could give. Sincerely
Meir Warcel
Look into your OpenCV directory;
For an example this; (installed using brew install opencv3 --with-java --with-python3)
/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
You will see;
libopencv_javaXXX.so opencv-XXX.jar
Now that you already have OpenCV's native library for Java (libopencv_javaXXX.so) compiled with you, the only thing left is, mac's dynamic library.
Link libopencv_javaXXX.so to libopencv_javaXXX.dylib;
ln -s libopencv_javaXXX.so libopencv_javaXXX.dylib
Now add /usr/local/Cellar/opencv3/XXX/share/OpenCV/java as Native Library Locations in IntelliJ or something similar in Eclipse.
Or add this to your JVM arguments;
-Djava.library.path=/usr/local/Cellar/opencv3/XXX/share/OpenCV/java
On a mac running OSX Yosemite, I dropped the libopencv_java2412.dylib file into /Library/Java/Extensions and it worked.
After you build opencv, the libopencv_java2412.dylib is generated in /build/lib.
After Spending a lots of time , and using different suggestions from StackOverflow I managed to get solution for windows. but I am adding a solution for mac as well. hope it should work.
Load your lib as per your system configuration.
private static void loadLibraries() {
try {
InputStream in = null;
File fileOut = null;
String osName = System.getProperty("os.name");
String opencvpath = System.getProperty("user.dir");
if(osName.startsWith("Windows")) {
int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
if(bitness == 32) {
opencvpath=opencvpath+"\\opencv\\x86\\";
}
else if (bitness == 64) {
opencvpath=opencvpath+"\\opencv\\x64\\";
} else {
opencvpath=opencvpath+"\\opencv\\x86\\";
}
}
else if(osName.equals("Mac OS X")){
opencvpath = opencvpath+"Your path to .dylib";
}
System.out.println(opencvpath);
System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll");
} catch (Exception e) {
throw new RuntimeException("Failed to load opencv native library", e);
}
}
2.now use this method as per your need
public static void main(String[] args) {
loadLibraries();
}
Building on Harsh Vakharia's answer i tried installing OpenCV on my mac with macports:
sudo port install opencv +java
ls /opt/local/share/OpenCV/java
libopencv_java343.dylib opencv-343.jar
To use this library I was hoping to be able to modify the library path at runtime which was discussed in
Adding new paths for native libraries at runtime in Java
And ended up with the following helper class and unit test. The code is now part of the
Self Driving RC-Car open Source project in which I am a comitter.
JUnit Test
/**
* #see <a href=
* 'https://stackoverflow.com/questions/27088934/unsatisfiedlinkerror-no-opencv-java249-in-java-library-path/35112123#35112123'>OpenCV
* native libraries</a>
* #throws Exception
*/
#Test
public void testNativeLibrary() throws Exception {
if (debug)
System.out.println(String.format("trying to load native library %s",
Core.NATIVE_LIBRARY_NAME));
assertTrue(NativeLibrary.getNativeLibPath().isDirectory());
assertTrue(NativeLibrary.getNativeLib().isFile());
NativeLibrary.load();
}
NativeLibrary
package com.bitplan.opencv;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.opencv.core.Core;
/**
* load OpenCV NativeLibrary properly
*/
public class NativeLibrary {
protected static File nativeLibPath = new File("../lib");
/**
* get the native library path
*
* #return the file for the native library
*/
public static File getNativeLibPath() {
return nativeLibPath;
}
/**
* set the native library path
*
* #param pNativeLibPath
* - the library path to use
*/
public static void setNativeLibPath(File pNativeLibPath) {
nativeLibPath = pNativeLibPath;
}
/**
* get the current library path
*
* #return the current library path
*/
public static String getCurrentLibraryPath() {
return System.getProperty("java.library.path");
}
/**
* Adds the specified path to the java library path
*
* #param pathToAdd
* the path to add
* #throws Exception
* #see <a href=
* 'https://stackoverflow.com/questions/15409223/adding-new-paths-for-native-libraries-at-runtime-in-java'>Stackoverflow
* question how to add path entry to native library search path at
* runtime</a>
*/
public static void addLibraryPath(String pathToAdd) throws Exception {
final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
usrPathsField.setAccessible(true);
// get array of paths
final String[] paths = (String[]) usrPathsField.get(null);
// check if the path to add is already present
for (String path : paths) {
if (path.equals(pathToAdd)) {
return;
}
}
// add the new path
final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
newPaths[newPaths.length - 1] = pathToAdd;
usrPathsField.set(null, newPaths);
}
public static File getNativeLib() {
File nativeLib = new File(getNativeLibPath(),
"lib" + Core.NATIVE_LIBRARY_NAME + ".dylib");
return nativeLib;
}
/**
* load the native library by adding the proper library path
*
* #throws Exception
* - if reflection access fails (e.g. in Java9/10)
*/
public static void load() throws Exception {
addLibraryPath(getNativeLibPath().getAbsolutePath());
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
Exception is occurring from below line of code:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Your program is trying to load a native library by the name of argument in call to loadLibrary method, which it is not able to locate. Make sure that native library (opencv.dll) is placed at one of the locations present in java.library.path system property as JVM looks at these locations for loading any native library (which might not contain 'java/jre/bin').
You can print java.library.path in your program like below:
System.out.println(System.getProperty("java.library.path"));
You cannot just put Windows library (dll file) on Mac and have it running - you need to compile the library for Mac first (or get Mac version of the library).
Please see here for tips on how to do it:
.dll Equivalent on Mac OS X
How do third-party libraries work in Objective-C and Xcode?
How to use a Windows DLL with Java in Mac OS X?
Instead of struggling with manual installation of OpenCV libraries I suggest you use OpenCV Java library packaged by OpenPnP (https://github.com/openpnp/opencv) that includes all required DLL.
It does not require additonal steps except of adding it to your build automation tool configuration (Gradle in my case) and adding the following code to load the library:
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
Just add into the path the folder where your opencv_java249.dll is; it would be something like C:\bin\opencv\build\java\x32 or C:\bin\opencv\build\java\x64 depending of your machine architecture. The problem is that java.library.path is actually the path variable.
netebans right klick project chosew properti
chose run, working direktory, click Browser change to opencv folder, release/lib,

Installation of javaCV [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to follow the instructions for installing javaCV from here: https://code.google.com/p/javacv/. I ve already built opencv. Actually I locate the java folder with .jar file and .dll in opencv folder and I add them in the path of my project. When I am trying to run the SimpleSample.java I am receiving Error: Could not find or load main class FaceRecognition. Is there something else I ve got to follow in order to install javaCV? My simple Code
package simplesample;
/**
*
* #author snake
*/
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class SimpleSample{
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
EDIT2: I add some jar files (javacv-windows-x86.jar, javacpp.jar, javacv.jar) and I finally got it running. But when I am trying to read a simple jpg image:
public static void main(String[] args) {
// TODO code application logic here
System.out.println("soul makosa");
IplImage image = cvLoadImage("ef.jpg");
if (image != null) {
cvSmooth(image, image, CV_GAUSSIAN, 3);
cvSaveImage("ef1.jpg", image);
cvReleaseImage(image);
}
}
I am reveiving the following errors:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_highgui in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1088)
at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:711)
at com.googlecode.javacpp.Loader.load(Loader.java:586)
at com.googlecode.javacpp.Loader.load(Loader.java:540)
at com.googlecode.javacv.cpp.opencv_highgui.<clinit>(opencv_highgui.java:79)
at projectcv.ProjectCV.main(ProjectCV.java:28)
Caused by: java.lang.UnsatisfiedLinkError: C:\Documents and Settings\chrathan\Local Settings\Temp\javacpp101399456657827\jniopencv_highgui.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1851)
at java.lang.Runtime.load0(Runtime.java:795)
at java.lang.System.load(System.java:1062)
at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:700)
... 4 more
Java Result: 1
I also add .dll file in properties->run->VM optimize as instructed here giving 'java.library.path' in netbeans for .dll/.so files. However I am still receiving the above message.
SOLVED:Ok my problems was due to the fact that I had to install specific version of javaCV. So for openCV version 2.4.6.1 I ve installed 0.6 java_CV and now works like a charm.
You need to add native class library path while adding opencv jar file into the project
Goto the buildpath>>libraries>>opencv >> click on expand and edit " native library location" to
C:/opencv/build/java/x86
for face FaceRecognition you need to add all jar files from that site
javacv-0.7-bin.zip
javacv-0.7-cppjars.zip
ffmg, etc....
it will work ....
Its working for me

how to use java native (cacls) for change file permission in windows

i had tried these syntax , but why it didn't work?
i really glad for your advice.
thanks before. sorry for bad english.
package priviledge;
import java.io.File;
import java.io.IOException;
/**
*
* #author DINA
*/
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException, IOException {
File f = new File("C:/lala/images1.jpg");
permission(f);
}
public static void permission(File src) throws InterruptedException, IOException {
// win32 command line variant
Process p = Runtime.getRuntime().exec("cacls 000 " + src.getPath());
p.waitFor(); // p.waitFor()
Because cacls works differently from chmod. In particular, the first argument is a file name (which would mean you try doing that on a file named 000). Windows using ACLs further comlpicates things as they don't fit as nicely into three octal numbers.
Look up the documentation of cacls (just type it at the command prompt) and fix your command line.
for cacls a group is the same as a user so for example to grant Full permission to the ABC group you would type "cacls filename /g ABC:F" You can view more details on the documentation of cacls
I was also working on a project which Involves changing Permissions of a folder. I tried many methods, but didn't work as I was expecting, So, I came up with a new Idea. For That you need basic knowledge about Batch files,
I wanted to change permission of a folder, So, This is what I did:
Created a text file, Wrote the commands in that file, Converted it into a batch file, and executed it using:
File file = new File("server.bat");
Desktop.getDesktop().open(file);
(Works for java 1.6 or newer), Deleted that batch file.
It may be a long way to do that, But in this method can execute any number of commands at a single shot. With this method, You don't have to worry about working directory as you can change it in the batch file. This may be a bad idea, I am new to java, But I told You my idea.

Eclipse v3.7.1 on OS X: Wacky compiling errors

I'm trying to compile Nachos OS in eclipse. The project compiles fine when I run "make" (which calls javac), but when I try to run it in eclipse, it spits out some bizarre java errors. For example, it is reporting that the last line of this javadoc comment is a file that it is unable to find...
Code Fragment:
/**
* The master class of the simulated machine. Processes command line arguments,
* constructs all simulated hardware devices, and starts the grader.
*/
public final class Machine {
/**
* Nachos main entry point.
*
* #param args the command line arguments.
*/
public static void main(final String[] args) {
System.out.print("nachos 5.0j initializing...");
Error Output:
/Users/dan/git/nachos/machine/Machine.java: line 19: */: No such file or directory
/Users/dan/git/nachos/machine/Machine.java: line 20: syntax error near unexpected token `('
/Users/dan/git/nachos/machine/Machine.java: line 20: ` public static void main(final String[] args) {'
I am using OS X 10.7.2 (Lion) with Java SE 6 (1.6.0 jdk).
I have configured the build settings to use Nachos' Machine.Machine.java main function.
Any idea why the odd output from Eclipse?
Seems like the project file got corrupted in the process of loading it onto a bitbucket server. I dropped eclipse like a bad habit in favor of IntelliJ. Soooo much happier now.

Categories

Resources