Java Powershell Exchangeonline in Tomcat directly doesn't work but using tomcat in netbean works

Kian-Lee Ng 21 Reputation points
2022-05-28T23:21:03.413+00:00

Hi,

I've the following java code to start a powershell script that connect using Connect-ExchangeOnline.

It works perfectly when invoking it from command line and from within a JSP with the tomcat within Netbean IDE.

However, if I run the same JSP in just tomcat itself (without Netbean) it hang with error timeout.

Do note, the environment I am running this code has a Proxy Server and all the necessary netsh winhttp set proxy xxxx has been performed.

Have anyone encounter this before?

Best Regards

StringBuilder sbInput = new StringBuilder();
StringBuilder sbError = new StringBuilder();

String[] commandList =
{
"powershell.exe", "-ExecutionPolicy", "Unrestricted", "-noprofile", "-noninteractive", "c:/temp/test.ps1"
};
ProcessBuilder pb = new ProcessBuilder(commandList);
Process proc = pb.start();
proc.getOutputStream().close();
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

String line;
while ((line = bufferedreader.readLine()) != null)
{
sbInput.append(line + "\n");
}

inputstream = proc.getErrorStream();
inputstreamreader = new InputStreamReader(inputstream);
bufferedreader = new BufferedReader(inputstreamreader);
while ((line = bufferedreader.readLine()) != null)
{
sbError.append(line + "\n");
}

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,173 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Joyce Shen - MSFT 16,641 Reputation points
    2022-05-30T02:52:41.377+00:00

    Hi @Kian-Lee Ng

    Glad to know that your issue is resolved now, and thanks for sharing the solution above! Since our forum has the policy that The question author cannot accept their own answer. They can only accept answers by others, and according to the scenario introduced here: Answering your own questions on Microsoft Q&A

    I would make a brief summary of this post so that other forum members could easily find useful information here:

    [Java Powershell Exchangeonline in Tomcat directly doesn't work but using tomcat in netbean works - Summary]

    Issue Symptom:
    The java code(which is used to start a powershell script that connect using Connect-ExchangeOnline) works perfectly when invoking it from command line and from within a JSP with the tomcat within Netbean IDE.
    However, when run the same JSP in just tomcat itself (without Netbean) it hang with error timeout.

    Solution:
    The issue is solved by dedicating an account in "This Account" under the "Log On" tab of the Tomcat "Services".
    206516-image.png

    You could "Accept Answer" for this summary to close this thread, and your action would be helpful to other users who encounter the same issue and read this thread. Thanks for your understanding!


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kian-Lee Ng 21 Reputation points
    2022-05-29T20:12:27.123+00:00

    I've managed to solve this issue by dedicating an account in "This Account" under the "Log On" tab of the Tomcat "Services". Not sure why though, but it works perfectly again. :) 206410-untitled.jpg

    0 comments No comments