Why Doesn't "System.Out.Println" Work in Android

Why doesn't System.out.println work in Android?

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.

Why shouldn't I use System.out.println() in android

You should use the android.util.Log class.

Here's a description of what the Log class does:

API for sending log output.

Generally, you should use the Log.v(), Log.d(), Log.i(), Log.w(), and Log.e() methods to write logs. You can then view the logs in logcat.

The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE. 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.

These are the available methods of the Log class:

  1. Log.d() - Send a DEBUG log message.
  2. Log.e() - Send an ERROR log message.
  3. Log.i() - Send an INFO log message.
  4. Log.v() - Send a VERBOSE log message.
  5. Log.w() - Send a WARN log message.
  6. Log.wtf() - What a Terrible Failure: Report an exception that should never happen.

The methods above (with the exception of Log.w and Log.wtf which have 3 possible patterns of arguments) require the following arguments:

  1. String tag, String msg:

    tag: Used to identify the source of a log message. This value may be null.

    msg: The message you would like logged. This value may be null.

  2. String tag, String msg, Throwable tr - Similar to the first pattern, but allows for an exception to be specified. This pattern should be used if you want to log an exception to the log output.

  3. (For Log.w and Log.wtf) String tag, Throwable tr Similar to the third pattern, but does not allow for a message to be specified. Note that you can still pass a message but it should be in the second arrangement of arguments.


EDIT: Going straight to answer your question: println() of System.out and System.err will still be displayed in logcat but with limitations.

  • You can't log VERBOSE, ERROR, or DEBUG using System.out or System.err.
  • You can't define your own tag, it will display System.err or System.out with your text. For instance:

    • System.out.println("Hello!") is equivalent to Log.i("System.out","Hello!")
    • System.err.println("Hello!") is equivalent to Log.w("System.err","Hello!")

string & system.out.println and other functions doesn't work

EDIT: Classnames starts with a capital letter on Java

System.out.println();

In this case you can also use the Logcat console,

Log.v(TAG, your_value_var);

I give you anothers comments and advices.

First of all I think you should download a Java language paper from the beginning, there you would see and you could clarify all these doubts and you could solve all the language problems.

Secondly I would advise you to download the latest version of Android Studio, the one you are using is a bit old, maybe with bugs and others, Android Studio 3, made many changes for our benefit.

And finally I advise you here Stackoverflow when you ask a question is because we have tried to solve the problem in many ways by other ways and we have tried many times, things like these are not wonder here, but we see that you are very new, and that's how we all started.

Welcome to the Community.

I hope it helps you improve.

Does Sytem.out.println work in Android?

System.out.println() works, also Log.d("TAG", "message"); works, but you need to open the LogCat view in eclipse and not the console view.

System.out.println doesn't work on a supplied Stream?

lineCount = Math.toIntExact(procLines.get().count());

count() is a terminal operation, it may traverse the stream to produce a result. After the terminal operation is performed, the stream pipeline is considered consumed, and can no longer be used.

So you consumed all the lines of the file. So, the supplier can't give you again the stream since BufferedReader is now at the end-of-stream. That's why there is no output.

Android studio does not print in new thread

You need to call .start() at the end

System.out.println("About to start new thread"); //this gets printed to the console
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Inside new thread"); //this doesn't get printed, why not?
}
}).start();


Related Topics



Leave a reply



Submit