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 8317845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:44:41+00:00 2026-06-08T21:44:41+00:00

What works: I have a c executable that runs a TUN/TAP service, and two

  • 0

What works:
I have a c executable that runs a TUN/TAP service, and two shell scrips (to configure the “ip route” and “iptables”) that run fine in terminal, all run as root.

What doesn’t work:
I am attempting to create an Android App to run the c executable and shell scripts after a button is pressed. I originally made it such that onClick would create a process with processBuilder as shown below:

final Button button1 = ...
...
public void onClick(View v) {
    String ip_address = edIPAddress.getText().toString();
    Process process;
    try {
        process = new ProcessBuilder()
            .command("/system/bin/su", "-c", "/data/tuntapserv/armeabi/mytunserv " + ip_address)
            .redirectErrorStream(true)
            .start();

        InputStream in = process.getInputStream();
        OutputStream out = process.getOutputStream();

        pOutput.append("TUN/TAP IS CONFIGURED!\n");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();             
    }
}

To the best of my knowledge this should have worked fine, but in practice mytunserv would stop working after a random period of time. mytunserv would start my TUN/TAP but then when I went to check the “ip route” after several minutes, I would see the tun/tap has stopped and disappeared. Similarly I tried runtime.exec, even though this seems to use processbuilder, and had the same issue.

I then moved onto using threads instead of processes, and this seems to work fine for the shell scripts.

...
final Button button2 = ...
...
    public void onClick(View v) {
        sThread = new ScriptThread();
        sThread.start();
    }
...

    private class ScriptThread extends Thread {
        @Override
        public void run() {
            Process process;
            try {
                process = new ProcessBuilder()
                    .command("/system/bin/su","-c","/system/bin/sh /data/tuntapserv/armeabi/setup_ip.sh")
                    .redirectErrorStream(true)
                    .start();

                InputStream in = process.getInputStream();
                OutputStream out = process.getOutputStream();

                pOutput.append("SCRIPT FINISHED!\n");

                while(true) {
                    Thread.sleep(0);
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();             
            }
        }
    }

This seemed to work for the two shell scripts at least. I eventually gave up on this method for calling mytunserv thinking the process or thread must be getting killed by the java thread handler, which seems pretty unpredictable from my research. Instead I moved onto JNI, since this seems more suitable than hardcoding my the path to the c executables and shell scripts.

So this is my most recent issue, and what I am looking for help on.

Here is my JNI setup:

#include <jni.h>
...
JNIEXPORT jint JNICALL Java_android_1sp_1api_sample_SetupTunTap_runtun(JNIEnv * env,jobject obj,jstring ip_address)
{
    jchar *ip;
    jboolean iscopy;
    ip=(*env)->GetStringUTFChars(env, ip_address, &iscopy);
    ...
    (*env)->ReleaseStringUTFChars(env, ip_address, ip);
    ...
}
...

The c code above turns into liblmytunserv.so.

package android_sp_api.sample;
...
public class SetupTunTap extends AnotherActivity {

    static 
    {
        System.loadLibrary("lmytunserv");
    }

    public native int runtun(String ip_address);
    ...

        final Button button1 = ...
    ...
    public void onClick(View v) {
        String ip_address = edIPAddress.getText().toString();

        SetupTunTap myTun = new SetupTunTap();

        myTun.runtun(ip_address);

        pOutput.append("TUN/TAP IS CONFIGURED!\n");
    }
...

From what I can tell, the c code should be able to call other functions in the c code that are not JNIEXPORT’ed. The JNIExport is just a way to have the Java code interact with the c code, from what I imagine.

Anyway, now my issue is that in debugging or running the app on the phone once I press button1 the activity closes and goes back to my main menu activity. In Debug it says “…. DalvikVM[localhost:8602]” and in DDMS the device stays “Online”. The LogCat does not output any thing to suggest that the activity exited/crashed. And upon entering the SetupTunTap activity LogCat resumes by displaying information but never outputs anything when button1 is pressed and the screen flashes black and reverts to my main menu activity. Here is the part, in the LogCat, at which the liblmytunserv is loaded:

LogCat Output:

...

07-30 16:30:19.531: D/dalvikvm(4716): Trying to load lib /data/data/android_sp_api.sample/lib/liblmytunserv.so 0x41688d20

07-30 16:30:19.531: D/dalvikvm(4716): Added shared lib /data/data/android_sp_api.sample/lib/liblmytunserv.so 0x41688d20

07-30 16:30:19.531: D/dalvikvm(4716): No JNI_OnLoad found in /data/data/android_sp_api.sample/lib/liblmytunserv.so 0x41688d20, skipping init

How do I debug further? If I set a breakpoint at “myTun.runtun(ip_address);” then I can see all the variables in the Debug interface on Eclipse. If I proceed past that point, the activity disconnects and I only see the .so lib file load in the LogCat. I would guess that my JNI setup must be wrong, so any advice anywhere in my thought process would be wonderful.

Hardware:
Samsung Galaxy Nexus v4.0.4 (Rooted)

Files:

  • mytunserv = c exectuable

  • setup_ip.sh = shell script

  • setup_ip1.sh = shell script

  • SetupTunTap.java

  • liblmytunserv.so (placed in /libs/armeabi, /libs/armeabi-v7a, /libs/x86)

What I need:
The native “runtun” function in the C library liblmytunserv.so runs forever in a while(1) infinite loop. When the Java code calls the function (once button1 is pressed), I need the function to run forever. Shell scripts setup_ip.sh and setup_ip1.sh are run once the c executable is running. How do I get the c executable to run forever?

Let me know if you need more information, thanks for your time.

  • 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-08T21:44:42+00:00Added an answer on June 8, 2026 at 9:44 pm

    In your JNI

    JNIEXPORT jint JNICALL Java_android_1sp_1api_sample_SetupTunTap_runtun(JNIEnv * env,jobject obj,jstring ip_address)
    

    in Activity

    package android_sp_api.sample;
    

    these need to match 😉

    edit:
    four out of five times it’s just this. also, post the java side as well, but I hope it looks like

    class SetupTunTap
    {
       public native int runtun(String ip_address);
    

    ///
    }

    so post SetupTunTap class as well\

    edit2: whoopse, the edit was unneeded. it’s just the package name.

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

Sidebar

Related Questions

So we have a java process that runs as a windows service. It needs
I have an executable, which runs fine when i run it manually, and it
Story: One of the app that i have works on python 2.4 and other
I have code that works great for adding a button to the toolbar: NSArray*
I have a asp.net web application that runs locally and opens docx files in
I have a Java executable that takes in command line arguments to launch root
We have an installation program that runs in Perl 32-bit. This program needs to
I have written an ASP.NET web page with C# behind that runs an existing
I have a 'long' python script that takes roughly 45[min] to run. I use
I have an executable on my web server that outputs XML to a file

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.