• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Installing Apache-Commons-net library on raspberry pi

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I developed much of a java program using JavaFX for use on a raspberry pi. In the process, I used an open-source apache library for some handy telnet methods. That library is here: https://commons.apache.org/proper/commons-net/download_net.cgi

My issue is that my code runs perfectly on my macbook, but crashes on the rPi. Commenting out the apache content allows it to run flawlessly on the Pi. I know I need to put the apache library jars somewhere on the rPi, but I have no idea where.

Any feedback is much appreciated.

Thanks.
 
Ranch Hand
Posts: 167
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to use an external lib in your application you just have to add it to the classpath - where this lies in the filesystem is up to you
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matt Wong wrote:if you want to use an external lib in your application you just have to add it to the classpath - where this lies in the filesystem is up to you



The library jars are imported into the project folder. They must be in the class path then, right?
 
Saloon Keeper
Posts: 2614
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

andrew mcnamara wrote:My issue is that my code runs perfectly on my macbook, but crashes on the rPi.


What does crashes mean?  Any error message, stack trace, etc.?
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this monolith to console:



Connecting to 10.18.20.119:22
cmd : cd '/home/pi/NetBeansProjects//WITPhoneHome'; '/opt/jdk1.8.0_211/bin/java'  -Dfile.encoding=UTF-8   -jar /home/pi/NetBeansProjects//WITPhoneHome/dist/WITPhoneHome.jar
Exception in thread "JavaFX Application Thread"
java.lang.NoClassDefFoundError: org/apache/commons/net/telnet/TelnetClient
at Path.<clinit>(Unknown Source)
at GUI.callConstructors(Unknown Source)
at GUI$5.lambda$handle$0(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3761)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3489)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2498)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:391)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.monocle.MonocleView.notifyMouse(MonocleView.java:119)
at com.sun.glass.ui.monocle.MouseInput.notifyMouse(MouseInput.java:314)
at com.sun.glass.ui.monocle.MouseInput.lambda$postMouseEvent$101(MouseInput.java:227)
at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:93)
at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:52)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.net.telnet.TelnetClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 53 more
"
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

andrew mcnamara wrote:The library jars are imported into the project folder. They must be in the class path then, right?



I don't know what "project folder" means. Is that something on your Pi?

But anyway when your command looks like "java ... -jar some.jar" then the only thing in the class path is "some.jar". If you need other jars in your classpath (as you do) then you need a command like "java Thing -classpath some.jar,other.jar,another,jar..." where Thing is the class you want to run.

Or alternatively, when you create "some.jar" you could put a "Class-Path" header in it which identifies the other jars which you need it to use.
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The project folder is the project directory on netbeans on my work station. I am compiling the project to pi in netbeans over SSH. I should have mentioned this.

Is it possible to add it to the class path so that all library jars are included in the jar which runs on the Pi?
 
Ron McLeod
Saloon Keeper
Posts: 2614
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Examine the contents of your jar and determine if the Apache classes are present.

For example:
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that apache jars are not in the runnable jar. Any advice as to where I should put them?

Many thanks for your help.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm unclear as to what you are trying to do. You have Netbeans on the Pi, right? So is your intention to develop this application there and then send a jar file over to the Macbook and run it there as well? Or are you developing on the Macbook?

I don't really like the idea of having Netbeans in both places for development, unless you have taken care of the issue of keeping the two development environments synchronized.

You shouldn't have to have Netbeans on the Pi if all you want to do there is to run your program there. All you should need to do is to copy all of the necessary jars over from the development environment.
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have Netbeans on the macbook. From netbeans on the macbook, I compile on the pi over SSH.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I sort of thought. Your folder names on the Pi made me think you had Netbeans over there, though.

Anyway what I would do is to transfer all of the needed jars over to the Pi -- WITPhoneHome.jar, the apache jar, and anything else you need. Put them all in the same folder. Which could be your dist folder if you like.

Then you can use class-path wildcards to say "All of the jars in this folder are in my classpath" instead of having to enumerate them.
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile over SSH to the Pi, netbeans creates a runnable jar on the Pi in the directory:

home/pi/NetBeansProjects/WITPhoneHome/dist/WITPhoneHome.jar

I try to put the apache jars inside the WITPhoneHome folder, but that doesn't work. Putting things in the dist folder also didn't work. Any other ideas?

Many thanks for your help.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Doesn't work"? Could you be a bit more specific?
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precisely the same error results when I run with the jars in the WITPhoneHome folder.
 
Ron McLeod
Saloon Keeper
Posts: 2614
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

andrew mcnamara wrote:I try to put the apache jars inside the WITPhoneHome folder, but that doesn't work. Putting things in the dist folder also didn't work. Any other ideas?


Is this a NetBeans issue then, where the libraries are not getting packaged in to your runnable jar?
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possibly a netbeans problem, but I assume I'm doing something wrong. I also tried to put the jars directly in the runnable jar and running on the pi from terminal. That resulted in the same errors.

To apply libraries, I right click the project folder --> properties --> Libraries (under "categories") --> add (the "+" sign) compile-time class path jar

Please somebody tell me I'm a dummy and I've done something simple and incorrect.

Screen-Shot-2019-07-12-at-5.11.21-PM.png
[Thumbnail for Screen-Shot-2019-07-12-at-5.11.21-PM.png]
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

andrew mcnamara wrote:Precisely the same error results when I run with the jars in the WITPhoneHome folder.



Well, as I said earlier, "java -jar yadayada.jar" isn't going to include any other jars in its classpath. Were you still using that version of the "java" command?

And jars inside a jar, which it sounds like you tried, aren't part of the classpath even if the containing jar is.

I don't know much about Netbeans but if you could get it to include a Class-Path header in the runnable jar it's generating, that would solve your problem. I don't know what that would look like in the component which generates the runnable jar but looking around at some of its options which you aren't using yet might help.
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was still using that version of the java command, yes.

What is a simple way to run our jar on the Pi through terminal while including multiple 3rd-party library jars in the class path?
 
Ranch Hand
Posts: 83
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Maven shade plugin. Nice and easy
 
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

andrew mcnamara wrote:What is a simple way to run our jar on the Pi through terminal while including multiple 3rd-party library jars in the class path?


This is a guess, but I would try putting the third-party jars in src/main/resources on the Pi.  That way you don't have to worry about a CLASSPATH.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

andrew mcnamara wrote:What is a simple way to run our jar on the Pi through terminal while including multiple 3rd-party library jars in the class path?



java -classpath /jarpath/* YourMainClass



Put all of your jars into the /jarpath folder. "YourMainClass" is the name of the class with the main() method which you want to run. Don't forget to put the package name before it.
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Paul : The Java file with the main method is called GUI.java, but the package name in NetBeans where all the source files are is simply “<default package>” (I know, bad move on my part, this is from a while ago). Is there a way we can tell that to the terminal (i.e. something like WITPhoneHome.GUI or main.GUI) since it won’t recognize “<default package>”?

$ java -classpath /home/pi/Desktop/Jars/ <default package>.GUI
-bash: default: No such file or directory
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the class isn't in a package then you just need to put the class name in that command. Like so:

java -classpath /home/pi/Desktop/Jars/* GUI



Don't forget the wild-card symbol to tell Java that the classpath includes all jars in that folder.
 
Bartender
Posts: 20838
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Common thread here.

NONE of the issues I see relate to the Raspberry Pi. They're all related to Java and to the dependence on an IDE. The main issue with the Pi is that as far as I can tell, it's NOT running the IDE, it's running the actual application. As it should.

Java does not ever look at jars that are inside other jars for classes. The only way to make it do so is by adding extra support code to the application or its framework. For example, webapp servers dig around in the WEB-INF/lib jars for class resources as well as the WEB-INF/classes directory for loose class resources.

To make a stand-alone executable jar work like that, as I said, some external magic has to be injected. That's what the Maven Shade plugin does when it builds an executable jar. In addition to copying the included jars into the product jar file, it also injects extra classloader logic into the app startup so that the contents of those included jars will appear on the app's classpath. Simply building with the java "jar" command isn't enough.

And just to make things perfectly clear: Java on the Raspberry Pi is no different than Java on any other hardware platform. It has exactly the same write-once/run anywhere rules as for the larger Linux desktop and server systems.

I say that as a person who right this moment is literally surrounded by Pi systems. I'm doing a home automation server project on them.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:NONE of the issues I see relate to the Raspberry Pi. They're all related to Java and to the dependence on an IDE.



This is absolutely true. Somebody added the thread to the Netbeans forum but yes, it's just another question about how to set the classpath.
 
andrew mcnamara
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Many thanks for your responses. Yes, I see now that this was purely a class path issue. I was able to resolve it last night by building a fat jar:

https://stackoverflow.com/questions/18451052/how-to-include-jars-in-lib-into-project-jar-file-in-netbeans/30172972

Your advice here led me to the solution.

Thanks again,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!