lakshayg
lakshayg

Reputation: 2173

Compiling OpenCV code in Java

I am new to java so maybe this is a little stupid. I have written a C++ code that uses OpenCV, now I want to convert it to Java. I used to compile C++ code in the terminal using

g++ main.cpp -o main `pkg-config opencv --libs` 

but I am unable to find a simple equivalent for java. I have tried using

javac -cp .:/usr/share/java/opencv.jar OpenCVDemo.java

but this gives a compilation error which looks like as if it was unable to link to the library. What I am looking for is a way to compile the opencv Java code in a way similar to the C++ method.

It would be helpful if someone could demonstrate how to compile this code

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Hello
{
   public static void main( String[] args )
   {
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
      System.out.println( "mat = " + mat.dump() );
   }
}

I have only been able to find ways to configure an IDE to compile the program or using ant or sbt. What I want is a way to compile the program from the command line using only javac.

Upvotes: 2

Views: 1317

Answers (1)

lakshayg
lakshayg

Reputation: 2173

Finally figured it out myself

Compiling:

javac -cp .:/usr/share/OpenCV/java/opencv-248.jar Hello.java

Executing

java -cp .:/usr/share/OpenCV/java/opencv-248.jar Hello

I think I was using the incorrect jar file

Upvotes: 1

Related Questions