Share via

Java.Lang.RuntimeException

Giorgio Sfiligoi 636 Reputation points
2025-08-02T08:17:13.26+00:00

I am developing an application that gets access to my OneDrive and downloads certain files; at the moment it is only a draft, for testing purposes - for Android only.

Here is a code snippet:

        try
        {
            //...
            // NOTE: 'MM_ID' is the Id of the folder that contains the file 'fileC'
            // 'graphClient' is obtained from the MSAClient library
            try
            {
                var stream_gmc = await graphClient.Drives["me"].Items[MM_ID]
                                                  .ItemWithPath(fileC)
                                                  .Content.GetAsync();
                if (stream_gmc == null)
                {
                    sb.AppendLine("Stream is null");
                    //** CREATE
                }
                else
                {
                    //** DO SOMETHING
                }
            }
            catch (ODataError ex)
            {
                if (ex.ResponseStatusCode == 404)
                {
                    sb.AppendLine("File not found");
                    //** CREATE
                }
            }
            //...
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            sb.Append("Exception: "); sb.AppendLine(ex.GetType().ToString());
            sb.AppendLine(ex.Message);
        }
        finally
        {
            result.Text = sb.ToString();
        }
    }

At times I get an exception of type Java.Lang.RuntimeException, with message "Exception_WasThrown, Java.Lang.RuntimeException".

This happens only when I test in the cellular phone (hence in Release mode), not with the Emulator in VS, and typically only the first time I try the download.

Can anybody explain and help me find a fix?

Developer technologies | .NET | .NET MAUI

Answer accepted by question author
  1. Harry Vo (WICLOUD CORPORATION) 4,750 Reputation points Microsoft External Staff Moderator
    2025-08-22T06:49:03.0733333+00:00

    Hi @Giorgio Sfiligoi ,

    Thank you for reaching out on Microsoft Q&A!

    The Java.Lang.RuntimeException you are seeing occurs only in Release mode on a physical device, and specifically on the first attempt to download a file via Microsoft Graph API might typically be caused by several reasons:

    1. In Release builds, the linker or Ahead-of-Time (AOT) compilation may remove certain code or assemblies that are required at runtime.
    2. On a physical device, network initialization or Graph API client setup may take slightly longer, causing the first call to fail intermittently.
    3. The Content.GetAsync() method may throw if the network stream or file stream is not fully initialized on the first attempt.

    Recommend solution: Implement a retry mechanism for the first download attempt. This ensures that if the call fails due to transient issues (network latency, initialization), it will automatically retry until success or until a timeout. You can do this easily by follow this example:

    public async Task<Stream> DownloadFileWithRetry(GraphServiceClient graphClient, string driveId, string filePath)
    {
        int maxRetries = 5;
        int delayMilliseconds = 500;
    
        for (int attempt = 1; attempt <= maxRetries; attempt++)
        {
            try
            {
                var stream = await graphClient.Drives[driveId]
                                              .Items[filePath]
                                              .Content
                                              .GetAsync();
                return stream; // Success
            }
            catch (Java.Lang.RuntimeException ex) when (attempt < maxRetries)
            {
                // Optionally log the exception
                await Task.Delay(delayMilliseconds); // wait before retrying
            }
        }
    
        throw new Exception("Failed to download file after multiple attempts.");
    }
    

    Additional Recommendations:

    1. Clean & rebuild the solution in Release mode to remove stale build artifacts (bin and obj folders).
    2. Check linker settings: Ensure required assemblies, like Microsoft.Graph, are preserved in Release mode.
    3. Add logging: Log failures and retry attempts for easier troubleshooting.
    4. Check network permissions: Ensure the physical device has internet access and proper storage permissions if writing locally.

    I hope this helps you get things back on track quickly! If my suggestions can solve your issue, feel free to interact with the system accordingly!

    Thank you!


1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.