I have a computer lab which is set up with piserver and 3B+ raspberry pis. I want my students to be able to write java code that will use the GPIO pins, so they will need to sudo java their programs. Unfortunately, sudo java any file is giving me a class not found exception. (it is a java course, we are going to be using pi4j and wiringpi, if only I can get even a simple program to run)

While troubleshooting the problem, I have simplified my program to a HelloWorld program and am trying to execute it using sudo.

I can run it with java HelloWorld without a problem.

When I try sudo java HelloWorld I get the class not found error.

When researching this I found that the user.home directory is set to /root ... and that it will need to be overridden (https://askubuntu.com/questions/659785/why-does-sudo-java-change-the-user-home-directory-to-root), so I tried this

java -Duser.home=$HOME HelloWorld

but, this does not fix the problem ... it still cannot find it (class not found error)

$HOME for me is /home/student

I have tried java -Duser.home=/home/student HelloWorld to no avail.

I am stuck. I have not been able to find anything else that addresses this issue.

  • You could try if the program runs without root when you add the user to the group gpio.
    – Bodo
    Commented Oct 12, 2021 at 19:07
  • Any modern GPIO library (less than 4 years old) should be able to run without sudo (using /dev/gpiomem).
    – Milliways
    Commented Oct 12, 2021 at 21:43
  • when referring to school programming course, please use the term java course ... using the term java class causes confusion
    – jsotola
    Commented Oct 12, 2021 at 22:54
  • What do you mean add the user to the group gpio? Run a program without root? I am not sure what you mean. I have tried to run the program that accesses the GPIO pins without sudo - it did not run. The user id that I log into the pi on is in the sudoers file. I actually was unaware that there were other java libraries that make accessing the GPIO pins possible, the p4ij library in conjunction with wiringpi definitely requires sudo according to their site pi4j.com/1.4/install.html ... and yes, I should have said course (not class)
    – KsMomOf2
    Commented Oct 13, 2021 at 1:23
  • I will try adding the group gpio and the user to that and see if that will work. This works with a previous version of pi4j/wiringpi on a stand alone pi. Would using the pi server be causing an issue with how the java program is executing?
    – KsMomOf2
    Commented Oct 13, 2021 at 1:40

1 Answer 1


FYI, WiringPi got deprecated in 2019 and doesn't support Pi4-boards. So please keep this in mind when creating your projects if students would use such a board at home for instance. Pi4J version 1.3 (Java 8) and 1.4 (Java 11), provide a work-around for this: https://pi4j.com/1.3/install.html#WiringPi_Native_Library

I would like to invite you to take a look at the getting started examples of Pi4J V.2 which is Java 11 and doesn't use WiringPi anymore: https://pi4j.com/getting-started/minimal-example-application/.

Choosing Java + Raspberry Pi + Pi4J is a great way to teach coding to students as it allows to combine soft- and hardware. The Swiss FHNW University is a leader in this approach as you can read on https://foojay.io/today/java-in-education-combining-java-with-raspberry-pi-and-the-pi4j-library/

Anyhow a long intro but this doesn't solve your problem (yet)...

Is your code published on GitHub? Can you do so and describe in the README which steps your students need to take to run the code? This way it would be possible to repeat your steps and come up with a solution.

  • 1
    While I think it is a laudable ambition to keep WiringPi working those involved need to become serious. The current implementation is based on a pre-Pi4 version of WiringPi. Those who claimed to have "fixed" it haven't even done any TESTING. Those parts which are identical to Pi3 work, but pullup doesn't work and PWM is also faulty. It is unfortunate that there is no other dynamically linked library. I did even consider doing this myself, but decided the code is just too convoluted and concluded that it would be easier to write a new library without the historical baggage.
    – Milliways
    Commented Oct 13, 2021 at 10:10
  • That's why Pi4J V2 was already some time ongoing to step away from WiringPi and be ready for future new boards... See pi4j.com/architecture/advanced/library-path. There will be no new V1 versions of Pi4J.
    – Frank
    Commented Oct 13, 2021 at 17:42
  • So - I did look into this. The school IT created a temporary piserver that I could take home and work on. I added the new version of pi4j (v2) and maven. But, you still need to be able to sudo to run the minimal example - which is exactly what I cannot do. I added the user to sudoers on the server and the user can run sudo commands, but when I try to sudo java a java program I still get the Could not find or load main class ... The problem is NOT pi4j, the problem is the piserver and the need to sudo to access the hardware. I can run java files, but I can't sudo java java files.
    – KsMomOf2
    Commented Oct 20, 2021 at 0:35
  • Indeed the sudo is needed for the GPIO access through PiGpio. I can't think of a solution for this immediately.
    – Frank
    Commented Oct 21, 2021 at 5:57
  • 1
    So, sudo is available to the students now, the problem is the folder (location of the .class file) and the use of the piserver. When I do this with a standalone pi, I have no problem. When I do this with the piserver, the location of where the java command is trying to find the .class file is root when using sudo java ... and even when I use an absolute path to the file it is not working. I think I am missing something in my understanding of how piserver works. Perhaps I should re-ask the question as a piserver question?
    – KsMomOf2
    Commented Nov 5, 2021 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.