snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

pi4j in a jar?

Thu Dec 13, 2012 12:29 pm

Hi,

I've got a simple java program using pi4j running without a gui, so I made a little gui for it in netbeans. The built jar file runs fine on the raspberry pi, until I try to import pi4j to the libraries of the gui project and use it.

I get this error on the pi:
[email protected] ~ $ java -jar "TestMachine.jar"
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
at testmachine.TestMachine.main(TestMachine.java:139)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
I have been creating and building the gui with netbeans on my mac and adding the pi4j.jar file into the libraries within that, then copying the built jar file to my pi.

I hope it's some sort of import/reference error. Can anyone please tell me what the problem is or how I can use pi4j in a jar/with a gui?

**Edit - after further reading it seems that it may be as simple as the library not being included when the jar is built?

I'd greatly appreciate it :)

User avatar
chriswhocodes
Posts: 37
Joined: Mon May 21, 2012 11:26 am
Location: London, UK
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 12:59 pm

Hi,

When you run an executable jar using

Code: Select all

java -jar foo.jar
you can set up the classpath in the jar's manifest file which is found in META-INF/MANIFEST.MF

It will look something like this:

Code: Select all

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_05-b05 (Oracle Corporation)
Main-Class: com.chrisnewland.FooLauncher
Class-Path: commons-codec-1.3.jar mina-core-1.1.7.jar
The easiest way to set this up is to build your jar with an ant script and use a task that automatically generates the manifest for you.

Here's a pretty minimal ant script that builds the manifest classpath for you:

Code: Select all

<project name="foo" default="all" basedir=".">
        <property name="src" value="../src" />
	<property name="build" value="../bin" />
	<property name="lib" value="../lib" />

<!-- scan the lib directory for jars and put into a variable classpath-->
	<path id="classpath">
		<fileset dir="${lib}">
			<include name="**/*.jar" />
		</fileset>
	</path>

<!-- convert it into manifest format (space separated)-->
	<pathconvert property="mf.classpath" pathsep=" ">
		<path refid="classpath" />
		<flattenmapper />
	</pathconvert>

	<target name="init">
		<delete dir="${build}" />
		<mkdir dir="${build}" />
	</target>

	<target name="compile" depends="init">
		<javac srcdir="${src}" destdir="${build}" classpathref="classpath">
		</javac>
	</target>

	<target name="jar" depends="compile">
		<jar destfile="${build}/foo.jar" basedir="${build}">
			<manifest>
				<attribute name="Main-Class" value="com.chrisnewland.FooLauncher" />
				<attribute name="Class-Path" value="${mf.classpath}" />
			</manifest>
		</jar>
	</target>

	<target name="all" depends="jar"/>

</project>
@chriswhocodes
http://www.chrisnewland.com/raspberrypi

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j in a jar?

Thu Dec 13, 2012 1:22 pm

Thanks for all that Chris, I'm grateful for the help :D

I'm trying to follow it now but I'm getting an error:
/Users/Me/NetBeansProjects/TestMachine/build.xml:14: /Users/Me/NetBeansProjects/lib does not exist.
Do I need to change something in your build xml code to direct it to the projects lib folder? /Users/Me/NetBeansProjects/TestMachine/dist/lib

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j in a jar?

Thu Dec 13, 2012 1:31 pm

I went ahead and replaced
<property name="lib" value="../lib" />
with
<property name="lib" value="../TestMachine/build/lib" />

and more errors have occurred..
run:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /private/var/folders/rj/qt8zbh7517q716xmgxr018x00000gn/T/libpi4j.so: dlopen(/private/var/folders/rj/qt8zbh7517q716xmgxr018x00000gn/T/libpi4j.so, 1): no suitable image found. Did find:
/private/var/folders/rj/qt8zbh7517q716xmgxr018x00000gn/T/libpi4j.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1939)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1864)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1825)
at java.lang.Runtime.load0(Runtime.java:792)
at java.lang.System.load(System.java:1059)
at com.pi4j.util.NativeLibraryLoader.loadLibraryFromResource(NativeLibraryLoader.java:326)
at com.pi4j.util.NativeLibraryLoader.load(NativeLibraryLoader.java:164)
at com.pi4j.wiringpi.Gpio.<clinit>(Gpio.java:165)
at com.pi4j.io.gpio.RaspiGpioProvider.<init>(RaspiGpioProvider.java:43)
at com.pi4j.io.gpio.GpioFactory.getDefaultProvider(GpioFactory.java:94)
at com.pi4j.io.gpio.impl.GpioControllerImpl.<init>(GpioControllerImpl.java:66)
at com.pi4j.io.gpio.GpioFactory.getInstance(GpioFactory.java:83)
at testmachine.TestMachine.main(TestMachine.java:139)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

User avatar
chriswhocodes
Posts: 37
Joined: Mon May 21, 2012 11:26 am
Location: London, UK
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 1:34 pm

Hi,

I use Eclipse and the standard project layout is:

myprojectdir
|--- src - source tree (.java files)
|--- lib - 3rd party jar files required by my code
|--- bin - output directory for .class files
|--- ant - where I keep my ant build scripts (hence the .. in the path to src, lib, bin)

If your build.xml (ant script) is at the same level as the src, bin, lib directories then just lose the .. from the properties in the top of the script.

E.g.

Code: Select all

<property name="src" value="src" />
<property name="build" value="bin" />
<property name="lib" value="lib" />
Just watch out (keep a backup) as the init task deletes and remakes the bin dir, so getting that wrong could potentially lead to deleting useful stuff :o

Cheers,

Chris
@chriswhocodes
http://www.chrisnewland.com/raspberrypi

User avatar
chriswhocodes
Posts: 37
Joined: Mon May 21, 2012 11:26 am
Location: London, UK
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 1:38 pm

A jar file is just a zip file with a different extension so please can you unzip your jar (that you built from your ant script) and show me what is in META-INF/MANIFEST.MF ?

Then compare this to your filesystem at runtime.

I assumed your own jar would be in the same directory as any 3rd party libs referenced in your manifest.
@chriswhocodes
http://www.chrisnewland.com/raspberrypi

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j in a jar?

Thu Dec 13, 2012 2:17 pm

I'm trying to upload screen shots of my filesystem and what is in the jar file but it's not happening

I can't build anything with the ant script because of the error:
/Users/Me/NetBeansProjects/TestMachine/build.xml:14: /dist/lib does not exist.

TestMachine
build.xml
build
dist/testMachine.jar
dist/lib
manifest.mf
src/testmachine/TestMachine.java
nbproject

apologies for being a pain in the backside

User avatar
chriswhocodes
Posts: 37
Joined: Mon May 21, 2012 11:26 am
Location: London, UK
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 2:54 pm

Hi,

No problems. Building jars with ant is a learning curve but you won't go back once you've mastered it :)

I think the problem is I'm trying to explain the ant way and you are using NetBeans which is probably trying to "help" you when it sees the build.xml

Maybe a NetBeans user can jump in and help here?

Alternatively, you could use my ant script and the command line ant tool to avoid conflicts with NetBeans.

Take a copy of your project folder and rearrange like this:

TestMachine
|--- build.xml
|--- bin(folder)
|--- lib (folder) contains 3rd party jars that you "import" in your code
|--- src/testmachine/TestMachine.java

And make sure your ant script properties are set to

Code: Select all

<property name="src" value="src" />
<property name="build" value="bin" />
<property name="lib" value="lib" />
then just go into TestMachine directory on the command line and type

Code: Select all

ant
(Ant will need to be on your "PATH" environment variable)

If that works you will have a jar file with a manifest that contains the classpath to your required 3rd party libraries. Now move your built jar file to the same folder as your 3rd party libs and you can run it using java -jar <jar file name>

Hope that helps.
@chriswhocodes
http://www.chrisnewland.com/raspberrypi

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 2:58 pm

Which version of Pi4J JAR are your trying to use? If using the 512mb Pi, you probably need the latest dev version of Pi4J available at:
https://oss.sonatype.org/content/groups ... -SNAPSHOT/
(look for the latest timestamp)
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 3:01 pm

You can also see in the "Execute" section of this page what command line argument I use to reference the Pi4J library when starting a Java program on the Pi.
http://pi4j.com/example/control.html
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j in a jar?

Thu Dec 13, 2012 3:08 pm

it's working, delighted :D

Thanks for taking the time Chris.

Stupidly I had pi4j in the same folder as the jar on the pi but I moved the pi4j jar into a lib folder in netbeans, so the final thing was to move pi4j into /lib folder on the raspberry pi.

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j in a jar?

Thu Dec 13, 2012 3:13 pm

savageautomate wrote:You can also see in the "Execute" section of this page what command line argument I use to reference the Pi4J library when starting a Java program on the Pi.
http://pi4j.com/example/control.html

I'm using 0.03, I did try that earlier but to be honest it was just a stab in the dark and I was probably referencing it incorrectly.

I gave this a go with python but I'm finding I like pi4j more, the gui seems a lot faster too.

User avatar
chriswhocodes
Posts: 37
Joined: Mon May 21, 2012 11:26 am
Location: London, UK
Contact: Website

Re: pi4j in a jar?

Thu Dec 13, 2012 3:22 pm

Glad you got it working :)
@chriswhocodes
http://www.chrisnewland.com/raspberrypi

robbanana
Posts: 5
Joined: Tue Apr 30, 2013 9:54 pm

Re: pi4j in a jar?

Tue Apr 30, 2013 11:15 pm

snooty wrote:it's working, delighted :D

Thanks for taking the time Chris.

Stupidly I had pi4j in the same folder as the jar on the pi but I moved the pi4j jar into a lib folder in netbeans, so the final thing was to move pi4j into /lib folder on the raspberry pi.
hey bro!, i need some help if I'm doing an interface whit netbeans, and i need import the pi4j package how i do that?, i mean when i writing on netbeans this:

Code: Select all

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPin;
netbean show me a message that i can not import them :S

robbanana
Posts: 5
Joined: Tue Apr 30, 2013 9:54 pm

Re: pi4j in a jar?

Tue Apr 30, 2013 11:40 pm

robbanana wrote:
snooty wrote:it's working, delighted :D

Thanks for taking the time Chris.

Stupidly I had pi4j in the same folder as the jar on the pi but I moved the pi4j jar into a lib folder in netbeans, so the final thing was to move pi4j into /lib folder on the raspberry pi.
hey bro!, i need some help if I'm doing an interface whit netbeans, and i need import the pi4j package how i do that?, i mean when i writing on netbeans this:

Code: Select all

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPin;
netbean show me a message that i can not import them :S
forget it bro, i already do! ; excuse my ignorance :S

Return to “Java”