Why doesn't "System.out.println" work in Android?
Solution 1
Correction:
On the emulator and most devices System.out.println
gets redirected to LogCat and printed using Log.i()
. This may not be true on very old or custom Android versions.
Original:
There is no console to send the messages to so the System.out.println
messages get lost. In the same way this happens when you run a "traditional" Java application with javaw
.
Instead, you can use the Android Log
class:
Log.d("MyApp","I am here");
You can then view the log either in the Logcat view in Eclipse, or by running the following command:
adb logcat
It's good to get in to the habit of looking at logcat output as that is also where the Stack Traces of any uncaught Exceptions are displayed.
The first Entry to every logging call is the log tag which identifies the source of the log message. This is helpful as you can filter the output of the log to show just your messages. To make sure that you're consistent with your log tag it's probably best to define it once as a static final String
somewhere.
Log.d(MyActivity.LOG_TAG,"Application started");
There are five one-letter methods in Log
corresponding to the following levels:
-
e()
- Error -
w()
- Warning -
i()
- Information -
d()
- Debug -
v()
- Verbose -
wtf()
- What a Terrible Failure
The documentation says the following about the levels:
Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.
Solution 2
Use the Log class. Output visible with LogCat
Solution 3
Yes it does. If you're using the emulator, it will show in the Logcat view under the System.out
tag. Write something and try it in your emulator.
Solution 4
it is not displayed in your application... it is under your emulator's logcat
Solution 5
if you really need System.out.println to work(eg. it's called from third party library). you can simply use reflection to change out field in System.class:
try{
Field outField = System.class.getDeclaredField("out");
Field modifiersField = Field.class.getDeclaredField("accessFlags");
modifiersField.setAccessible(true);
modifiersField.set(outField, outField.getModifiers() & ~Modifier.FINAL);
outField.setAccessible(true);
outField.set(null, new PrintStream(new RedirectLogOutputStream());
}catch(NoSuchFieldException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
RedirectLogOutputStream class:
public class RedirectLogOutputStream extends OutputStream{
private String mCache;
@Override
public void write(int b) throws IOException{
if(mCache == null) mCache = "";
if(((char) b) == '\n'){
Log.i("redirect from system.out", mCache);
mCache = "";
}else{
mCache += (char) b;
}
}
}
Related videos on Youtube
TIMEX
Updated on July 08, 2022Comments
-
TIMEX almost 2 years
I want to print something in console, so that I can debug it. But for some reason, nothing prints in my Android application.
How do I debug then?
public class HelloWebview extends Activity { WebView webview; private static final String LOG_TAG = "WebViewDemo"; private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webview = (WebView) findViewById(R.id.webview); webview.setWebViewClient(new HelloWebViewClient()); webview.getSettings().setJavaScriptEnabled(true); webview.setWebChromeClient(new MyWebChromeClient()); webview.loadUrl("http://example.com/"); System.out.println("I am here"); }
-
Samuh over 14 yearsThough you have got your answer below,I would like to add that output of SOP statements are directed to LogCat too: only that the tag name would be System.out
-
Charlie Collins over 13 yearsBefore 0.9 System.out was lost, I think. After it was passed to the logcat output: code.google.com/p/android/issues/detail?id=92. (If you're using an existing library or such that uses System.out, right or wrong, it will show up in logcat with later versions of Android.)
-
rDroid over 12 yearswe can see the System.out.printlns inside the logcat.
-
-
kaspermoerch over 12 yearsActually - System.out prints to LogCat through Log.i().
-
David Webb over 11 years@JosephEarl - Feel free to use the Edit button.
-
Zorb about 10 yearsthere is also Log.wtf() :-)
-
Byron Gavras about 9 yearsAdditional to this answer bear in mind that some times "something happens" and no input is printed with the usage of System.out. If this the case try to close and restart the emulator. This worked for me.
-
PVS over 8 yearsSystem.out.println does show in Android Studio, under Android Monitor. These are shown as "I/System.out"
-
PaulMcG about 8 yearsThe new Console add-in library is also very useful.
-
Admin almost 7 yearsWhen I click on
Android Monitor
and thenlogcat
, I see many many messages constantly being printed... Is it supposed to be like this? Then how would I see my own debug messages? -
Valentin Filyov almost 7 yearsI just scroll through the other things and find it :D
-
Zoe stands with Ukraine over 6 yearsIt's worth noting that Log.wtf can throw an exception when used. It shouldn't be used unless there is a case that never should happen in the first place
-
Farid almost 5 yearsThis is the very first sentence of the OP "I want to print something in console"
-
Michał Jaroń over 2 yearsThere is a race when reading/writing variable LogUtil.log. You should use e.g. condition variable or monitor to synchronize variables and avoid active waiting loop.