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.
Related
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
I'm rank new to bash thus the question.
I've a java program that I've exported as a .jar.
This file when run as
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
How do I write a bash script to do automated testing of this project.
I need the scrip to do the following -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
This should do it.
#!/bin/bash
files="$#"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename it first. Then call it like this:
./filename firstfile secondfile thirdfile etc.
Other:
As sjsam pointed out, the use of <<< is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
1st using executable jar
2nd by running java class from terminal
create a new text file and name it hello.sh
In hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
1 navigate to directory where your HelloWorld.jar is present
2 give permission to terminal to run the bash script by using the following command
sudo chmod 754 hello.sh
3 run you script by running the following command
./hello.sh
I have error while executing java command with .sh file with external library.
I have wrote a script called executer.cmd which contains
java -cp .;hsql.jar hsqlconnector %*
its working fine with windows.
For Unix also I have wrote a script and make u+x with chmod but still m getting error
of
bash: hsql.jar command not found
My executor.sh looks like
java -cp .;hsql.jar hsqlconnector %*
On Linux you must use : (colon) instead of ; (semi-colon) to separate entries on a path, because ; has a different meaning in the shell on Linux.
See here:
http://www.coderanch.com/t/526784/Linux-UNIX/cp-linux-include-additional-jar
I have created a exe file from jar via converter tools. Jar file was executing fine when I tried to run via unix by passing input parameters eg: java -jar SSS_Infinite.jar test.in 2
However after converting to exe I tried to run by passing input parameters via Unix but its not working and simply returns to the next line. I tried the below command in Unix cmd. Is there any other alternative to make it trigger ?
SSS_Infinite.exe test1.in 2
I assume you created executable for Windows platform, it will not work on *nix systems.
The simplest option will be to build little script that will accept parameters and pass them to java -jar, something like that:
#!/bin/bash
java -jar SSS_Infinite.jar $1 $2
where $1 and $2 are script arguments, see explanation here.
after you create that script and save it as say SSS_Infinite.sh, change its permissions:
chmod +x SSS_Infinite.sh
Then you'll be able to execute it like that:
./SSS_Infinite.sh test1.in 2
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.