How to ... do it all! (USB support)

84 views
Skip to first unread message

Bill Lee

unread,
Aug 15, 2019, 9:27:40 AM8/15/19
to usb4java
Hello; (status alert: newby here!)

I've been doing software development since the dark ages, but just dived off into Java a few weeks ago. A big learning curve but not really a problem.

Now I am to the point of needing USB support and apparently that is a waste-land in Java! The one bug report I was able to find in the openJDK was 12 years old and had no action since. COME ON! Is Java a real player? No intrinsic USB support?

The project I am working on was first written in c++, is heavily object-oriented and uses a wrapper of my own creation around libusb. The application works, but making it available to Windows users is a royal PITA. The "run anywhere" advertising for Java lead me here.

I have browsed through the usb4java website, I have downloaded the latest release, but I find NO discussion or instructions on how to make this software available to my needs. I look at sample of code and all reference javax.usb which simply doesn't exist in the openJDK that I have installed (standard Fedora release). I don't know how to install the usb4java code.

Can someone give me some links, some pointers, ... anything! .... that will let me install  usb4java in such a manner that I can compile and test USB access? That when the time comes to package the application for my Windows friends that I don't have a major problem there?

Thanks in advance.

Regards,

Bill Lee


Alex Gardiner

unread,
Aug 15, 2019, 9:49:41 AM8/15/19
to usb4...@googlegroups.com
Hi Bill

If your USB is an HID, try PureJavaHIDAPI instead. It worked for me


Alex

--
You received this message because you are subscribed to the Google Groups "usb4java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to usb4java+u...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/usb4java/c60a8cc9-20d3-4790-9aa4-cc04409908ff%40googlegroups.com.

Bill Lee

unread,
Aug 15, 2019, 10:29:25 AM8/15/19
to usb4java
Thanks, Alex, for the prompt response.

I do not think my device could be called an HID.

The system consists of a visual sensor that "sees" things. When the sensor senses an event, it transmits a pulse to a small "timer box". That box time stamps the event and provides a USB interface. That is where I come in with my application: I must be able to send control messages to the timer box, I must be able to get the time stamped events, etc.

Regards,

Bill

Bill Lee

unread,
Aug 15, 2019, 3:11:48 PM8/15/19
to usb4java
O.k., here's an illustration of the kind of help I need.

I have a directory JavaProgramming/USB

I have downloaded the examples that are referenced on the usb4java website: " low-level (libusb) examples "

I have downloaded the "usb4java (Main library)" also linked on the site.

All of this now in the JavaProgramming/USB directory.

Now what? I want to compile the examples. How do I get the Main Library unzipped somewhere so that the compiles can use it?

Any help greatly appreciated.

Regards,

Bill Lee


Peter Stoiber

unread,
Aug 15, 2019, 4:10:26 PM8/15/19
to usb4java
Hello Bill,

i would suggest you to use Net Beans for your Linux Distro.
On Netbeans open a new Project with Maven support. Maven is on Net Beans quite good included and there are a lot of samples out there.
On the official usb4java web site do all the stuff to acces an usb device on linux (you need to gain extra acess on linux for an usb device) (udev rules).
Include maven and usb4java in your pom.xml somthing like this:



 <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<dependency>
        <groupId>org.usb4java</groupId>
        <artifactId>usb4java</artifactId>
        <version>1.3.0</version>
    </dependency>

<build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>2.3.2</version>
              <configuration>
                  <encoding>${project.build.sourceEncoding}</encoding>
              </configuration>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-resources-plugin</artifactId>
              <version>2.4.3</version>
              <configuration>
                  <encoding>${project.build.sourceEncoding}</encoding>
              </configuration>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-dependency-plugin</artifactId>
              <version>3.1.1</version>
              <executions>
                  <execution>
                      <id>copy-dependencies</id>
                      <phase>package</phase>
                      <goals>
                          <goal>copy-dependencies</goal>
                      </goals>
                      <configuration>
                          <includeScope>compile</includeScope>
                      </configuration>
                  </execution>
              </executions>
          </plugin>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.1.1</version>
          </plugin>
      </plugins>
      <sourceDirectory>src</sourceDirectory>
  </build>
</project>


All in all this is the easiest way to work with usb4java on linux. I also have a running sample of usb4java on linux mint, which I could share to you, if you don't get the samples running this way.

So far,

Peter

Bill Lee

unread,
Aug 17, 2019, 10:35:38 PM8/17/19
to usb4java

Hello,Peter;

Thanks for your response. I had held off on Netbeans, I come form the old school of text editors and compiler and linkers and gdb. All that "new fangled IDE stuff" was yet another learning curve, and at 77, I don't have a lot of time for many more! :-)

But,....

I did install Netbeans on my system and have begun using it. I was able to build my system as a Netbeans project, I was able to get Maven to work and the Netbeans project converted to a Maven project, I was able to establish a dependency to the usb4java system. Now it's time to go forward with the conversion of the libusb C++ code into my Java system.

Thanks for your guidance, I have made progress because of it.

Regards,

Bill Lee


Peter Stoiber

unread,
Aug 18, 2019, 6:03:53 AM8/18/19
to usb4java
Hello Bill,

to test LibUsb C++ code you can also use NetBeans. On NetBeans 9 or higher you have to add C/C++ support manually: https://stackoverflow.com/questions/51493882/netbeans-9-c-support/51576217
Thirst LibUsb has to be installed on your system:

sudo apt-get install libusb-1.0-0-dev
 
Then open a new C/C++ Project (A lot of samples are out there) which creates the native .so library which you can then include in your java sample.
On the C/C++ Project in NetBeans include your java JMV directories;
Project Properties
Build
C Compiler // C++ Compiler
General / Include Directories: (On Mint) /usr/lib/jvm/java-8-oracle/include    /usr/lib/jvm/java-8-oracle/include/linux
 
Under Additional Options add: "-lusb"

Under Project Properties --> Linker --> Libraries add the libusb library under following link (on Mint):
/usr/lib/x86_64-linux-gnu/libusb-1.0.so
or the archive:
/usr/lib/x86_64-linux-gnu/libusb-1.0.a

Next write your C / C++ program header and source files and include libusb: #include <libusb-1.0/libusb.h>

You can build then C / C++ libraries with "build, or cean and build" which were loaded from your java Program the following way:
You can specify the path and name of the c / c++ lib under: Project Properties --> --> Linker --> Output

static { 
        System.load("/Path_to_Netbeansfolder/NetBeansProjects/Path_to_Native_Project_Output_Lib_Folder/Library.so");
    }
 
and run the native methods this way:

private native void nativeMethod();

I have only included LibUsb over Jni for now and this was the way doing it. You can also search the web for other ways.

So far,

Peter
Reply all
Reply to author
Forward
0 new messages