run java.exe with arguments from powershell - java

I'm trying to call wlst/jython/python from powershell
set classpath with setWLSEnv.cmd is not set in the right session? so I have tried to set -cp as argument
& C:\bea\tpc\weblogic1033\server\bin\setWLSEnv.cmd;
$cp='C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar'
$wlst='weblogic.WLST'
$script='C:\domains\tpc\Domain\bin\status.py'
$java="C:\PROGRA~1\Java\JROCKI~1.0\bin\java"
& "$java $cp $wlst $script"
#or
. "`"$java`" -cp `"$cp`" $wlst `"$script`""
#or
& "`"$java`" -cp `"$cp`" $wlst `"$script`""
I have tried to quote the command string in various ways without success
The term '"C:\PROGRA~1\Java\JROCKI~1.0\bin\java" -cp "C:\bea\tpc\WEBLOG~1\server\lib\weblogic.jar" weblogic.WLST "C:\domains\tpc\SasTT
pcDomain\bin\status.py"' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of
the name, or if a path was included, verify that the path is correct and try again.
At C:_WORK_\SAS\statusAll.ps1:15 char:2
+ . <<<< ""$java" -cp "$cp" $wlst "$script""
+ CategoryInfo : ObjectNotFound: ("C:\PROGRA~1\Ja...\bin\status.py":String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

When you use the call operator &, the next token needs to be the name of a command and nothing else. So instead of this:
& "$java $cp $wlst $script"
Try this:
& $java $cp $wlst $script
Sometimes getting arguments to native exes can get ugly. A technique that usually works but is unsafe if any of your arguments come from user input is this:
Invoke-Expression "$java $cp $wlst $script"

In addition to the trouble with how you're formatting your command into a string, setWLSEnv.cmd is a script for the Windows Command Prompt. PowerShell cannot execute this file; it does not know how to interpret it, much like how Notepad does not know how to interpret a docx file. Windows associates .cmd files with the command prompt, so your first line is equivalent to
& cmd.exe /c C:\bea\tpc\weblogic1033\server\bin\setWLSEnv.cmd
(Note that the semicolon is unnecessary since you don't have any other commands on the same line.)
This creates a new process using cmd.exe. This new process executes the batch file, setting the environment variables you expect, and then the process exits, discarding the environment changes.
If you need setWLSEnv.cmd to set environment variables prior to executing your program, you should write a batch file that calls it instead of a PowerShell script. Otherwise, you will need to find or write a PowerShell equivalent to set up your environment.

Related

Running java executable with powershell within command prompt

I have a powershell script that is working and runs a java executable. Before I was generating a bunch of powershell script files that were run through the command prompt. Now I want to make it so there does not need to be file creation.
Here is what the line looks like from the working (.ps1) file:
java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe
I want to be able to do something like this in command prompt:
Powershell -Command "java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe"
Even just asking this question I am having problems with the escape characters. What is the proper way to handle escape characters when you have quotes in quotes in quotes when calling java through powershell through command prompt? (I understand it is a bit messy)
You can lessen the quoting headaches if you focus just on the parts that require quoting (assuming that option value true truly needs quoting):
REM From cmd.exe
C:\> powershell -Command java -Doption1=3 -Doption2="'\"true\"'" jar.exe
The above will make java.exe see:
java -Doption1=3 -Doption2="true" jar.exe
As you can see, even this simple case of the desired resultant quoting is quite obscure, because you're dealing with 3 layers of interpretation:
cmd.exe's own interpretation of the initial command line
PowerShell's interpretation of the arguments it receives.
How PowerShell's translates the arguments into a call to an external program (java.exe).
In any event, the final layer of interpretation is how the target program (java.exe) parses the command line.
That said, in order to call java.exe, i.e. an external program, you don't need PowerShell at all; invoke it directly:
REM From cmd.exe
C:\> java -Doption1=3 -Doption2="true" jar.exe

Translating windows bat file to linux shell script

This is my exact batch file. I have tried to convert it doing some research online and get an error
"Failed to execute child process "/home/pi/Desktop/TeachVal/TeachValLinuxShell" (No such file or directory)
echo off
cls
echo Running TeachVAL II...
set path=%path%;/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/bin
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
cls
exit
This one is my attempt at translating.
#!/bin/bash
set +v
clear
echo "Running TeachVAL II..."
java -cp ".dir1;dir2;path/home/pi/Desktop/TeachVAL/comm.jar;
path/home/pi/Desktop/TeachVAL/Robot.jar;/home/pi/Desktop/TeachVAL/TeachVAL"
clear
exit
Welcome to Linux--life is good here, but there are a few things that work slightly differently, when compared to Windows.
One difference is that Windows uses semicolon (;) to separate entries in a list of paths, but Linux uses colons (:) for that purpose.
So, the Windows command:
java -classpath comm.jar;Robot.jar;TeachVAL TeachVAL
would correspond to this on Linux:
java -classpath comm.jar:Robot.jar:TeachVAL TeachVAL
In general, on Linux, semicolons are used to put multiple command lines into a single line. Once you've learned that, I think you can then understand why:
java -cp .dir1;/home/pi/Desktop/TeachVAL/TeachVAL
would be the same as:
java -cp .dir1
/home/pi/Desktop/TeachVAL/TeachVAL
That would run java (with no class to be executed) and then try to run "/home/pi/Desktop/TeachVAL/TeachVAL" which can't be found.
There are many more differences to learn; here's a page that will help you get started: http://tldp.org/LDP/abs/html/dosbatch.html

Command Prompt Custom Code Error

At my company, we have users that manually type in code into command prompt line by line. I was trying to create something that would allow them to paste one line of code, but I get an error in the code when executing.
Here is my Code:
D: & cd b2borders & cd jar b2b & cd bin & set path=%path%;c:\Program Files (x86)\java\jre6\bin & java -jar ejecutable.jar & pause
Here is my error:
'java' is not recognized as an internal or external command, operable program or batch file.
Note: When I execute the code line by line, no errors.
Please advise!!!
Thanks,
Doug F.
As you are asking for a one line copy paste you could shorten your code thus:
CD /D "D:\B2Borders\Jar B2B\bin" & "%ProgramFiles(x86)%\java\jre6\bin\java.exe" -jar "ejecutable.jar" & Pause
You may also find that an alternative using Start works:
Start "" /D "D:\B2Borders\Jar B2B\bin" "%ProgramFiles(x86)%\java\jre6\bin\java.exe" -jar "ejecutable.jar" & Pause
To save your end users opening a Command Prompt themselves you could even try, (as entry into run box):
Cmd /K "Start "" /D "D:\B2Borders\Jar B2B\bin" "%ProgramFiles(x86)%\java\jre6\bin\java.exe" -jar "ejecutable.jar""
You are missing an s at C:\Program File**s**(x86)\java\jre6\bin in your screenshot.
But you seemed to have typed correctly in the question.
I'd suggest that as you are changing path in your cascaded command, the original value would be used in an attempt to locate java.exe, hence the error message.
The most obvious solution would be to establish a batch file to perform most of the retyping - manually entering that command would be error-prone, with the possibility of disastrous consequences if it is mistyped.
However, if you specify the entire executable name, "c:\Program Files (x86)\java\jre6\bin\java.exe" which would need to be within "double quotes as shown" because of the separators in the path, then the command should be executed properly. What java would think is yet another matter...

UNIX: java program command running from terminal but not working from bash script

This command works great from terminal $java -jar $picard
Whenever I put that in bash script, It gives error as: line 2: -jar: command not found
#!/bin/sh
$java -jar $picard`
Is there any fix, thanks?
The $ at the beginning of the line is not part of the command; it is part of the shell prompt.
When you put the command in a batch file, you should not include the shell prompt. So change it to:
#!/bin/sh
java -jar $picard
EDIT
OP mentioned that "$java" points to the actual Java binary.
If you are following naming conventions for shell scripts then $java and $picard are local variables in your shell, not environment variables, so they don't get passed onto any commands that you invoke.
To turn them into environment variables, you need to export them from your shell. Whereever you set values in them is the best place to put:
export java
export picard
However, this turns them into environment variables, and in that case you should make the names "all capitals" -> JAVA, PICARD.
You have to export the java variable which you are using to point to java path as below
JAVA_CLASSPATH=/bin/jre1.8.0_101/bin/java
export PICARD_CLASSPATH=/bin/picard-tools-2.5.0/picard.jar
$JAVA_CLASSPATH -classpath $PICARD_CLASSPATH {Nmae of the Class from where the execution begins}
Instead of using small case you can use capital letter so it will be more readable.

How do I deal with Spaces in the $PSScriptRoot path in powershell

I am trying to write a powershell script that will Install Java using an Installcfg.cfg file. I would like it to be able to run no matter where the script is executed. It works fine when there are no spaces in any of the file paths however when I run it from another location I get the following error:
There are errors in the following switches:
"INSTALLCFG='C:\Program";"Files";"(x86\DesktopCentral_Agent\swrepository\1\swuploades\Java";"8";"Update";"25\InstallCFG.cfg'";.
Powershell does not appear to be passing the file paths as I want them.
The script is as follows:
#Determine the Architecture
$Arch = (Get-WmiObject Win32_OperatingSystem).OSArchitecture
#Make sure no browsers are running
kill -processname iexplore, chrome, firefox
#Install based on Architecture
if ($Arch -eq "64-bit")
{
Start-Process jre-8u25-windows-x64.exe -wait -args "INSTALLCFG=$PSScriptRoot\InstallCFG.cfg /L c:\temp\java64.txt"
Start-Process jre-8u25-windows-i586.exe -wait -args "INSTALLCFG=$PSScriptRoot\InstallCFG.cfg /L c:\temp\java32.txt"
}
else
{
Start-Process jre-8u25-windows-i586.exe -args "INSTALLCFG=$PSScriptRoot\InstallCFG.cfg /L c:\temp\java32.txt"
}
I have tried various ways of including quotes in the arguments and even creating the arguments as a variable. I do not want to hard-code the location because I want the file to run from anywhere.
I am basically trying to run the following command from a batch file in powershell:
"%~dp0jre-8u25-windows-x64.exe" INSTALLCFG="%~dp0InstallCFG.cfg" /L c:\temp\log.txt
What am I missing?
The escape character is backtick. You should be able to write something like
"INSTALLCFG=`"$PSScriptRoot\InstallCFG.cfg`" /L c:\temp\java64.txt"
for the args argument.

Categories

Resources