3

I have a Java SE Application that use

input = new Scanner(System.in);

to get Input parameters

and use System.out.println("..");

to print results

Since all Java APIs used in the original Java Project are also available in Android, I have tried to import all classes without any error, but now I don't know how replicate the behaviour of the classic Java console in Android.

I have seen that there are developers that have achieved this in some IDE-like apps, but I don't know how.

Could you help me?

Example: assume that you want to port this dummy Java SE Application in Android mantaining the console-like approach of the original code

    public static void main(String[] args) {
         System.out.println("Please enter your choice");
         System.out.println("A, B");

         Scanner myScanner = new Scanner(System.in);
         String choice = myScanner .nextLine();
         if (choice.charAt(0) == 'A') {
                ...do something
         }
         else{
                ...do something
         }

}
AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • You should be able to see the results from `System.out.println("..");` in the logcat. I've never tried using System.in – Matthew Wesly Oct 25 '13 at 17:21
  • @MatthewWesly I know, but my question is different. I need to replicate the console behaviour... there are developers that have done what I have asked in their apps but I don't know how they have achieved this – AndreaF Oct 25 '13 at 17:28
  • have you checked this http://stackoverflow.com/questions/8673016/building-a-terminal-emulator-for-android – Niko Oct 25 '13 at 17:41
  • Are you trying to make something like this http://www.appbrain.com/app/android-terminal-emulator/jackpal.androidterm? – Niko Oct 25 '13 at 17:42

1 Answers1

0

You can execute system commands with exec(). Here is how to do it:

Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a"); // here goes your input
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";    
while ((line = b.readLine()) != null) {
    System.out.println(line);
}

Update:

Ok, from what I understand, you would like to compile and run code written by user. I can see 3 options:

  1. Most difficult I think. Get the source code of some Java compiler and include it in your project. So, user inputs a text. You compile it, run and give the output.

  2. Using already built compiler. This requires root. Install javac on your device. Then, in your application you can call it with the above exec() code.

  3. Easiest one. Using internet and for example, Ideone.com. In your app you send code to compile on Ideone. You get back the output and present it to the user.

Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • Thanks for the answer but I haven't understand how should I use this to achieve my goal(the console should get the input from something like an EditText and print the output (in a TextView or another EditText), to replicate the standard Java console (not the Linux Shell). The original program that I'm trying to port is a Java SE application that use only Java console to do everything. Could you give me more details? – AndreaF Oct 25 '13 at 18:26
  • @AndreaF Ah, I understand better now. See my update. – Adam Stelmaszczyk Oct 25 '13 at 18:59
  • I don't want to write a compile and run app like an IDE. I have a Java SE application that use the standard console for input and output and I want to port It in Android without rewrite too much code. All the classes are yet compatible with android, the only problem is that I don't know how replicate the console behaviour easily because I never dealt with this kind of need. – AndreaF Oct 25 '13 at 20:16