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

  • Home
  • SEARCH
  • 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 8167349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:09:49+00:00 2026-06-06T20:09:49+00:00

so I wrote a Java program to change the IP of a machine. public

  • 0

so I wrote a Java program to change the IP of a machine.

public static void main(String[] args) {
    superUserChangeIp(args);
}

public static void superUserChangeIp(String[] args) {
    try {

        String ethInterface = args[0].toString().trim();
        String ip = args[1].toString().trim();

        System.out.println(ethInterface + " " + ip);

        String[] command = {
                "/bin/sh",
                "ifconfig " + ethInterface + " " + ip };

        Process child = Runtime.getRuntime().exec(command);

        changeNetworkInterface(ethInterface, ip);

        readOutput(child);

        child.destroy();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

I made a simple sh script :

#!/bin/sh
sudo java -jar changeip.jar $1 $2

and I’m getting : /bin/sh: 0: Can't open sudo ifconfig eth0 192.168.217.128 trying to run sh ipconfig.sh eth0 192.168.217.128

Anyone can point me to the right direction?

Note: the method changeNetworkInterface(ethInterface, ip); simply updates the settings found in /etc/network/interfaces, code follows :

protected static void changeNetworkInterface(String ethInterface, String ip) throws IOException {
    String oldInterface = File.readFile(NETWORK_INTERFACES_PATH);
    StringBuilder builder = new StringBuilder();
    Scanner reader = new Scanner(oldInterface);

    boolean startReplacing = false;

    while (reader.hasNextLine()) {
        String currentLine = reader.nextLine();

        if (currentLine.contains("iface " + ethInterface)) {
            System.out.println("Found interface!");
            startReplacing = true;
        }

        if (startReplacing && currentLine.contains("address")) {
            System.out.println("Found IP!");
            currentLine = "address " + ip;
            startReplacing = false;
        }

        builder.append(currentLine);
        builder.append("\n");
    }

    System.out.println(builder.toString());

    File.writeToFile(NETWORK_INTERFACES_PATH, builder.toString());
}
  • 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-06T20:09:50+00:00Added an answer on June 6, 2026 at 8:09 pm

    If you’re running the JAR as you described, the application will already have superuser privileges, so you don’t need to run sudo inside the app at all.

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

Sidebar

Related Questions

I'm trying to convert an old Delphi program I wrote into Java to compile
I wrote a program in java that rolls a die and records the total
I wrote a simple program in java web forms but i am receiving the
I have wrote the code below which was taken from Java How to program
I recently wrote a java crawler program that finds the video links in a
in detail: if we use public API for example, write java program for example
I was using a logger, java.util.logging.Logger , in my program. First I wrote something
I have to write a Java program that tells what coins to give out
I need to write one java program which monitors a folder containing excel sheets
I want to write a java program that acts as a user interface to

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.