Question

I have written some code in C which I want to call from Java. What I have done is I have a function in my c code which calls another c function from libspotify and I am trying to write a Java Wrapper using JNA to call my c function.

I have written a simple login function for spotify in c which is actually working. Here is a link to that c file which has the method

https://github.com/shroffrushabh/libspotify_java/blob/master/jukebox.c

The next step I followed is, instead of using the make file provided in the libspotify examples I used the following cmd to compile and create a .so file

gcc -o libspot.so -shared jukebox.c appkey.c

This is what my java file looks like

import com.sun.jna.Library;
import com.sun.jna.Native;

public class SpotifyTest {
    public interface JukeBox extends Library {
        public int login();
    }
    static public void main(String argv[]) {
        JukeBox jk = (JukeBox) Native.loadLibrary("spot", JukeBox.class);
        jk.login();
    }
}

The following are the commands to compile and run the java file

javac -classpath jna-4.0.0.jar SpotifyTest.java
java -classpath jna-4.0.0.jar:. SpotifyTest

Following is the exception I get:

java.lang.UnsatisfiedLinkError: /home/rushabh/libctest.so: undefined symbol: sp_session_create

I am not sure what is going wrong here, but here is what I am thinking, there is a sp_session_create function in libspotify(the C api which spotify has provided) which I am calling in my c code, so I think I need to somehow link the libspotify library when I am trying to generate my .so file. It would be great if you could give me some suggestions on how to solve this problem

Was it helpful?

Solution 2

after 2 days of researching this is how I got it to work, following is the command I used to generate the .so file.

gcc -L~/Downloads/libspotify-12.1.51-Linux-i686-release/lib  -shared appkey.c jukebox.c  -lspotify -o libspot.so

Rest of the steps to execute to Java file are same as above.

Thank you Erick Robertson and Petesh for your help :)

OTHER TIPS

You need to link in the spotify library.

This should be done when you're compiling libspot.so. This can be done with:

-L/path/to/spotify/library

This should be something/libspotify/lib, and is likely the directory where you built libspot.so. You may also need to specify:

-I/path/to/spotify/includes
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top