Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:33:15+00:00 2026-06-04T12:33:15+00:00

I cannot get a Java application to run correctly when starting it with sudo.

  • 0

I cannot get a Java application to run correctly when starting it with sudo. My application uses sockets for client connections and also establishes a database connection. If I start the app normally:

java myApp

Everything works perfectly. However, I am trying to get this to be able to be started in the background from a website. So I created a PHP to start it using exec() or passthru(). At first, this seemed to work, but I soon discovered the sockets weren’t actually connecting. Further trial and error led me to the conclusion this was because when I do java myApp, I am logged in as root, but the PHP is not executing as root so there’s security issues.

So I wrote a script, set SUDOERS to allow the script to be run without a password. The script has a single line, java myApp. If I run the script with:

bash myScript

The sockets won’t connect. (Which is to say the applet client hangs trying to connect.) If I try:

sudo bash myScript

The jdbc MySql driver fails to be found. I tried various methods to cure this, including messing with the classpath including using -classpath at compile and / or runtime and putting a copy of the jdbc jar in different places but no luck. Apparently, the sudo environment is different in some subtle way from the logged-in-as-root environment which causes Java to not be able to find the jdbc drivers.

To be clear, I get the jdbc ClassPathNotFound (or no suitable driver error, depending on how I attempt the connection) when I try to run sudo bash myScript while logged in as root. bash myScript works just fine on all counts logged in as root. When I try to run the the either from a PHP script, my client applet can’t connect, which could either be the sockets or jdbc. I am not using an IDE of any sort, just editing with text editors and using javac to compile.

Any suggestions would be appreciated.

Update:

The server is running CentOS, if that helps. I figured the -E was an environment thing (and had guessed environment loss was the cause, but the sudo documentation I was finding wasn’t helping me figure out the solution). I have tried passthru('sudo -i bash /path/to/myScript.sh'); but the java app still fails to start. Nothing shows up in the browser from the passthru. The script now reads:

cd /path/to/app
java myApp

Still toying with it myself; I know I am close to making this work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:33:17+00:00Added an answer on June 4, 2026 at 12:33 pm

    After discussion via the comments I understand, that sudo -E works (for your username) – this seems to be the giveaway to what goes on:

    Your interactive logon has lots of environment variables set up, some of which are needed for execution of Java apps.

    Normally, sudo will “clean up” your environment before switching user – so it will remove the necessary environment variables, but sudo -E will keep the environment intact, making it work for you.

    Now the webserver user (e.g. www-data in Ubuntu) has no environment set up, making sudo -E keep a defective environment.

    There are a few ways to address that, the easiest being to use sudo -i – this will run the target user’s shell as if on login.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally got this calculator application in Netbeans to compile correctly and run client-side
I cannot get the example Python programs to run. When executing the Python command
I'm building an Eclipse RCP application against Eclipse 3.7.1 and Java 1.6. I'm also
In my main project in IntelliJ I cannot get my selenium tests to run.
i have developed application while running my application another application ContactManager is also get
I cannot get my SID working ... <?php session_start(); // Or maybe pass along
I cannot get Bindable LINQ to work with VB.NET for the life of me.
I cannot get the background to stretch behind the contentbox. The strange thing is,
I cannot get the jQuery slideDown function to work on <table> elements. They just
I cannot get the DropDownHeight of the ComboBox set properly to display all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.