Share via

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");
}

Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Joyce Shen - MSFT 16,706 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.

Was this answer helpful?

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

    Was this answer helpful?

    0 comments No comments

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.