getControllers() gives an empty array

I’m having much trouble running JInput with a gamepad. When I call getControllers() it returns an empty array

following is my code:

[quote]/*

  • Bestuuring.java
  • Created on 12 mei 2005, 10:14
    */

package idp1;

import net.java.games.input.;
import net.java.games.util.plugins.
;

/**
*

  • @author Groep15
    */
    public class Bestuuring extends Thread {
    private net.java.games.input.Controller gp;

    public Bestuuring() {
    System.out.println(“bestuuring online”);
    ControllerEnvironment ce = DirectInputEnvironmentPlugin.getDefaultEnvironment();
    // retrieve the available controllers
    net.java.games.input.Controller[] controllers = ce.getControllers(); //this gives an empty array
    if (controllers.length == 0) {
    System.out.println(“controllers == null”);
    } else {
    System.out.println(“controllers found”);
    }
    net.java.games.input.Controller c;
    gp = null;
    for (int i = 0; i < controllers.length; i++) {
    c = controllers[i];
    if(c.getType() == net.java.games.input.Controller.Type.GAMEPAD) { // c is a gamepad
    gp = controllers[i];
    }
    }
    }

    public void run() {
    //not yet coded, no need because the constructor doesn’t work
    }

    private void errorHandler(String message) {
    Main.stuur.append(“e” + message); //works fine
    }
    }
    [/quote]
    I’m running on Windows XP with NetBeans 4 and J2sdk1.4.2

Could someone please give me some advice?

Grtz, Foppie

The msot common cause of seeing no controlerls is nto having the paltform plugin properly installed.

I’ve re-installed the driver and rewritten my code, it seems to me the driver is properly installed now. But another problem has appeared ???, so I’ve not tested it yet. Can anybody help me with my next problem?

The output I received:

[quote]init:
deps-jar:
Compiling 1 source file to D:\School\Project Telebluppy\GUI\build\classes
compile:
OS name is: Windows XP
DX8 plugin is supported
OS name is: Windows XP
DX8 plugin is supported
java.lang.NoSuchMethodError: addAxis
at net.java.games.input.DirectInputEnvironmentPlugin.directInputCreate(Native Method)
at net.java.games.input.DirectInputEnvironmentPlugin.(DirectInputEnvironmentPlugin.java:114)
at idp1.Bestuuring.(Bestuuring.java:22)
at idp1.Main.main(Main.java:71)
Exception in thread “main”
Java Result: 1
debug:
BUILD SUCCESSFUL (total time: 10 seconds)
[/quote]
The error is thrown on the first line of my constructor

[quote]public Bestuuring() {
DirectInputEnvironmentPlugin diep = new DirectInputEnvironmentPlugin();
/* This throws an Internal Exception:
* java.lang.NoSuchMethodError: addAxis
* at net.java.games.input.DirectInputEnvironmentPlugin.directInputCreate(Native Method)
* at net.java.games.input.DirectInputEnvironmentPlugin.(DirectInputEnvironmentPlugin.java:114)
*/

    // retrieve the available controllers	
    net.java.games.input.Controller[] controllers = diep.getControllers();
    gp = null;
    if (controllers.length > 0) {//error is caught in the run methode
        net.java.games.input.Controller c;
        for (int i = 0; i < controllers.length; i++) {
            c = controllers[i];
            if(c.getType() ==  net.java.games.input.Controller.Type.GAMEPAD) { // c is a gamepad
                gp = controllers[i];
            }
        }
    }
}

[/quote]

That would apper to be caused by the native libraries you have been out of sync with the java libraries.

Kev

I’m a newbie so could you give me some tips on solving this?

I can try :wink:

To start with, where did you get your java and natives from?

I’ve put the last versions of the jar and natives that seem to work on most platforms, here:

http://www.cokeandcode.com/jinput

Be warned! These are not an official release and are nowhere near up to date with the current code base. However, they are the last consistent builds that seem to work and have natives for each platform.

Kev

I used the files from the NightlyBuilds. But after using your files I get the same problem.

I also searched my code for any addAxis method call (for the third time) but I can not seem to find where this method is called…

addAxis would be being called from inside the C code for the direct X plugin.

Try out: http://www.newdawnsoftware.com/jinput/jinput.jnlp

Its a webstart demo that attempts to use JInput, it works happily here. Also, could you let us know what version of java you’re using (including the full version number) since some releases had significant JNI (Java Native Interface) issues.

Kev

This works fine, but if I use it in my code, it (again) gives the same error ???

my Java Version: Version 1.4.2_08 (build 1.4.2_08-b03)

Hmm… now thats interesting… the libraries used in the webstart version are actually the same ones I’ve put at cokeandcode…

Ah ha, go back to your original code with the new libraries. You musn’t directly instance the DirectInputEnvironementPlugin like that.

Could you repost the code that you’re using after.

Kev

the complete code of the thread. The code in sendAxis() might have some errors, but I’m sure my problems are not coming from there, because he can’t come in that code, because I get the errorMessage “controller is null” on screen

package idp1;

import net.java.games.input.Axis;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.DirectInputEnvironmentPlugin;

public class Bestuuring extends Thread {
    private net.java.games.input.Controller gp;
    
    public Bestuuring() {
        ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment(); 
        // retrieve the available controllers	
        net.java.games.input.Controller[] controllers = ce.getControllers();
        gp = null;
        if (controllers.length > 0) {//error is caught in the run methode
            net.java.games.input.Controller c;
            for (int i = 0; i < controllers.length; i++) {
                c = controllers[i];
                if(c.getType() ==  net.java.games.input.Controller.Type.GAMEPAD) { // c is a gamepad
                    gp = controllers[i];
                }
            }
        }
    }
    
    public void run() {
        while (true) {
            if (gp == null) {
                errorHandler("controller is null");
            } else {
                if (gp.poll()) {
                    sendAxis(gp.getAxes());
                    errorHandler("");
                } else {
                    errorHandler("controller invalid");
                }       
            }
        }
    }
    
    private void sendAxis(Axis[] assen) {
        for (int i = 0; i < assen.length; i++) {
            float data = assen[i].getPollData();
            errorHandler(assen[i].getName());//for debugging only!
            if (assen[i].getIdentifier() == Axis.Identifier.X) {
                if (data > 0) { //arrow right
                    sendButton(7);
                }
                if (data < 0) { //arrow left
                    sendButton(8);
                }
            }
            if (assen[i].getIdentifier() == Axis.Identifier.Y) {
                if (data < 0) { //arrow up
                    sendButton(5);
                }
                if (data > 0) { //arrow down
                    sendButton(6);
                }
                /* alternative buttons for up and down
                if ((assen[i].getName() == "Knop 5") || (assen[i].getName() == "Knop 7")) {//up, R1 and R2 buttons
                    sendButton(5);
                }
                if ((assen[i].getName() == "Knop 4") || (assen[i].getName() == "Knop 6")) {//down, L1 and L2 buttons
                    sendButton(6);
                } 
                */
            }
            if (assen[i].getIdentifier() == Axis.Identifier.BUTTON) {
                if (assen[i].getName() == "Knop 0") {//button 1
                    sendButton(1);
                }
                if (assen[i].getName() == "Knop 1") {//button 2
                    sendButton(2);
                }
                if (assen[i].getName() == "Knop 2") {//button 3
                    sendButton(3);
                }
                if (assen[i].getName() == "Knop 3") {//button 4
                    sendButton(4);
                }
            }
        }
    }
    
    private void sendButton(int button) {
        Main.stuur.append(String.valueOf(button)); // not "k" + button
    }
    
    private void errorHandler(String message) {
        Main.stuur.append("e" + message);
    }
}

Ok, now we’re getting somewhere. Takeout the gamepad check for now, since your gamepad may be being reported as something else. Also you might consider just dumping out the size of the controllers array at the start (I know you’ve done that before, just for this version ;))

Then let us know what you’re getting on the console, including the junk JInput dumps out.

Sorry this is taking so long :frowning:

Kev

the complete console output:

init:
deps-jar:
Compiling 1 source file to D:\School\Project Telebluppy\GUI\build\classes
compile:
controllers == null
debug:
BUILD SUCCESSFUL (total time: 32 seconds)

and teh editted code:

    public Bestuuring() {
        ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment(); 
        // retrieve the available controllers	
        net.java.games.input.Controller[] controllers = ce.getControllers();
        gp = null;
        if (controllers.length == 0) {
               System.out.println("controllers == null");
           } else {
               System.out.println("controllers found");
           }
        if (controllers.length > 0) {//error is caught in the run methode
            net.java.games.input.Controller c;
            for (int i = 0; i < controllers.length; i++) {
                c = controllers[i];
                //if(c.getType() ==  net.java.games.input.Controller.Type.GAMEPAD) { // c is a gamepad
                    gp = controllers[i];
                //}
            }
        }
    }

I should be apologizing to you that it is taking so long :wink:

Ah no, not the build console, what comes out to the console when you run it? Unless of course your ant task called “compile” is actually execute?

If so, where are your native libraries with respect to where you’re running from?

Kev

I don’t have much more consoles.
This is the other, the debugging console:

[quote]Listening on 4582
User program running
User program finished
[/quote]
my own code is in a package called idp1
the libraries are in net/java/games/input and net/java/games/util/plugin
(were in .jar files, but that didn’t work so I tried it extracted, with the same result. On the moent it is still in extracted state)
an overview:
-Source Packages
—idp1
------Bestuuring.java
------Main.java
------<more files, witch I think don’t matter>
—net.java.games.input
------
—net.java.games.input.test
------
—net.java.games.util.plugin
------
—net.java.games.util.plugin.test
------

Ah, but where is the dll?

And where are you actually running from? Java must be able to find the DLL to actually use it access your controllers.

Kev

as said in the JInput Introduction sticky on top of this forum, I placed the dll as following:

[quote]Installation of the plugin(s):

Nearly all plugins consist of two files. One of them is the native library (e.g. dxinput.dll, libjinput.so) and the other one is the java part of the plugin (any official name for that?). There are multiple ways for installing a plugin:

  1. the easiest but most (system) polluting way:
    create a folder named ‘controller’ inside your ${jre.home}/lib/ext folder and copy the two files in.
    [/quote]
    The project itself is located on another harddrive, but that was never a problem and I don’t know why it should be a problem now…

Ah, now this gets all murky :slight_smile:

You need to have the jar and dll in the same location. So the dxinput.jar needs to be in there with the dll and the source needs to be removed from your directory. Really you should be working off the jars…

What was the problem when you tried to work with the jars?

Kev

the dxinput.jar is in ${jre.home}/lib/ext/controller/ together with the dll

I never extracted that jar, only the jars in the applications classpath (jinput.jar and jutils.jar). The reason was to see if it would work extracted. But it did not make a difference (as you would propably have expected ;))

Ok, still can’t work out why the plugin isn’t being picked up. If you just want to get up and running, you could try setting the System Property “jinput.plugins” to the name of the plugin class you want to use before you use the default environment,


System.setPropery("jinput.plugins","net.java.games.input.DirectInputEnvironmentPlugin");
...
ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment(); 

This forces the plugin to be picked up but in a controlled manner but its not the recommended way of doing things. This should also check if the class is actually visible to the VM.

Kev