I have taken state plane coordinates from a SQL Server table and populated an object with 100 concatenated strings that will serve as my input into a java application.
$key = Invoke-Sqlcmd -Query "SELECT DISTINCT CONCAT('spc,'<spcZone>,',',
<northing>,',',<easting>, ',',<units>,',',<inDatum>,',',<outDatum>) as
conversionString FROM <source table>;" -ServerInstance "<server>" -Database
"<database>"
The result will be 100 strings that look like this in an array:
spc,2402,173099.419,503626.812,m,NAD83(2011),NAD83(2011)
I then use NOAA's gtk java application and run through all 100 observations to be converted:
$result = FOREACH ($k in $key.conversionString)
{
java -Dparms="$k" -jar H:\gtk\jtransform_thin.jar
}
The returned output for one observations looks like this:
{
"ID":"1489004917960",
"nadconVersion":"5.0",
"srcLat":"40.0000000000",
"srcLatDms":"N400000.000000",
"srcLon":"-80.0000000000",
"srcLonDms":"W0800000.000000",
"destLat":"40.0000000000",
"destLatDms":"N400000.000000",
"destLon":"-80.0000000000",
"destLonDms":"W0800000.000000",
"sigLat":"0.000000",
"sigLon":"0.000000",
"srcEht":"100.000",
"destEht":"100.000",
"sigEht":"0.000",
"srcDatum":"NAD83(1986)",
"destDatum":"NAD83(1986)",
"spcZone":"PA S-3702",
"spcNorthing_m":76470.584,
"spcEasting_m":407886.482,
"spcNorthing_usft":250887.241,
"spcEasting_usft":1338207.566,
"spcNorthing_ift":250887.743,
"spcEasting_ift":1338210.243,
"spcConvergence":"-01 27 35.224524",
"spcScaleFactor":0.99999024,
"spcCombinedFactor":0.99997455,
"utmZone":"UTM Zone 17",
"utmNorthing":4428236.065,
"utmEasting":585360.462,
"utmConvergence":"00 38 34.174932",
"utmScaleFactor":0.9996897,
"utmCombinedFactor":0.99967402,
"x":849623.061,
"y":-4818451.818,
"z":4078049.851,
"usng":"17TNE8536028236"
}
The problem I'm encountering is accessing the stored $result object's returned fields. If I type $result. It will return all text from all 100 observations. If I type $result[1] I only get the ID of the first observation. If I type $result.ID I do not have anything returned.
This is where I am trying to get to:
$add = foreach ($r in $result)
{
"INSERT INTO SPCtoLatLong VALUES ('" + $r.spcZone + "','" +
$r.spcNorthing_usft + "','" + $r.spcEasting_usft + "','" + $r.srcLat + "','"
+ $r.srcLon + "','" + $r.srcDatum + "','" + $r.destDatum + "')" + $nl
}
I only have 2 weeks experience in PowerShell, what am I doing wrong? Thank you for any help.
Stdout from external applications is returned as a string array (one line = one enum). You would need to parse the return data into an object, or, if you're on PowerShell 5+, you can utilize the ConvertFrom-String to create templates for these objects and pass it output to it to be converted.
But! Based on how well-formed your data is, I would do the following:
$Expected = $Result | ConvertFrom-Json
PS C:\> $Expected.GetType()
PSCustomObject
I figured out to solve this problem you should add this to the java application execution component.
$result = FOREACH ($k in $key.conversionString)
{
java -Dparms="$k" -jar H:\gtk\jtransform_thin.jar | ConvertFrom-Json
}
I was struggling with the following:
when i use the path of python program in the normal desktop, java can run the python program in eclipse.
However, i use the path of python program which is in the tomcat, java cannot run the python program in eclipse.
// String callPyPath="C:\\Python27\\python C:\\Users\\Desktop\\myprogram.py"+" ";
// GOOOOD it work!!!
String callPyPath="C:\\Python27\\python C:\\Program Files\\Apache Software Foundation\\Tomcat 8.0\\webapps\\testWeb\\WEB-INF\\classes\\com\\myprogram.py"+" ";
// BAD it cannot work!!!
String addKeyWord1=KeyWord1+" ";
String addsourcePath=sourcePath+" ";
String addKeyWord2=KeyWord2+" ";
String saveresultPath="C:\\Users\\Desktop\\results\\";
String cmd = callPyPath+addKeyWord1+addsourcePath+addKeyWord2+saveresultPath;
You have to escape the space in your path.
String callPyPath="C:\\Python27\\python C:\\Program\ Files\\Apache\ Software\ Foundation\\Tomcat\ 8.0\\webapps\\testWeb\\WEB-INF\\classes\\com\\myprogram.py"+" "
Or replace the spaces after the creation of the String:
String callPyPath="C:\\Python27\\python C:\\Program Files\\Apache Software Foundation\\Tomcat 8.0\\webapps\\testWeb\\WEB-INF\\classes\\com\\myprogram.py"+" "
callPyPath.replace(" ", "\\ ");
Hello my problem is as follows:
i have a property fiel with a startconfig for external program to run from java:
# Standardauswahl falls keine PlayerType übergeben wurden
Default = 1
# Liste der gültigen PlayerTypes
PlayerTypes = Human,MCTS,TMM,Random,Value
StartConfig = \"C:\\Program Files\\Java\\jdk1.7.0_13\\bin\\javaw.exe\" -Dlog4j.configuration=file:///C:/Users/djdeejay/git/myGit/com.djdeejay.cowTrade.client.standaloneplayer.application/bin/log4j.xml -Dfile.encoding=Cp1252 -classpath [..... some parameter deleted.....] 0.0-RC1.jar;C:\\Users\\djdeejay\\git\\myGit\\de.thWildau.cowTrade.server\\lib\\slf4j-api-1.5.2.jar;C:\\Users\\djdeejay\\git\\myGit\\de.thWildau.cowTrade.server\\lib\\slf4j-log4j12-1.5.2.jar;C:\\Users\\djdeejay\\git\\myGit\\de.thWildau.cowTrade.server\\lib\\log4j-1.2.16.jar com.djdeejay.cowTrade.client.standaloneplayer.application.RandomPlayerApplication %1 %2 %3
when i load the properties as follows
cmd = this.serverSettings.getPlayerTypeSetting("StartConfig");
#Override
public String getPlayerTypeSetting(String key) {
return this.startPlayerTypeSettingsProp.getProperty(key);
}
java cutting startconfig after the first space:
Cannot run program """C:\Program" when its in doublequotes or Cannot run program ""C:\Program":
i have tried several variants with "", with escaping and so on nothing gives my needed result
how do i config getproperty to read until EOL?
cheers
As discussed in the comments, the getProperty call is alright. But the usage as a single string in Runtime.getRuntime().exec(cmd) uses the default whitespace Tokenizer to split the string into command and argument.
To do this yourself, first split the string manually and then pass it to Runtime#exec:
String startCmdLine = this.serverSettings.getPlayerTypeSetting("StartConfig");
int cmdEndPos = startCmdLine.indexOf("javaw.exe") + "javaw.exe".length();
String cmd = startCmdLine.substring(0, cmdEndPos);
String args = startCmdLine.substring(cmdEndPos);
Runtime.getRuntime().exec(new String[]{ cmd, args });
Though I really advise you to take advantage of the JAVA_HOME environment variable if you just want to start a known java binary. That way you can reduce that to a set of parameters in the property file.
Try replacing your spaces with: "\u0020"
Use this:
StringEscapeUtils.escapeXml(String input);
See doc here
I am trying to use the OptionBuilder.withArgName( "property=value" )
If my Option is called status and my command line was:
--status p=11 s=22
It only succeeds to identify the first argument which is 11 and it fails to identify the second argument...
Option status = OptionBuilder.withLongOpt("status")
.withArgName( "property=value" )
.hasArgs(2)
.withValueSeparator()
.withDescription("Get the status")
.create('s');
options.addOption(status);
Thanks for help in advance
You can access to passed properties using simple modification of passed command line options
--status p=11 --status s=22
or with your short syntax
-s p=11 -s s=22
In this case you can access to your properties simply with code
if (cmd.hasOption("status")) {
Properties props = cmd.getOptionProperties("status");
System.out.println(props.getProperty("p"));
System.out.println(props.getProperty("t"));
}
If you need to use your syntax strictly, you can manually parse your property=value pairs.
In this case you should remove .withValueSeparator() call, and then use
String [] propvalues = cmd.getOptionValues("status");
for (String propvalue : propvalues) {
String [] values = propvalue.split("=");
System.out.println(values[0] + " : " + values[1]);
}
I'm using graphviz to generate graphs based on the messages passed in a scala program.
To invoke the graphviz application from inside the scala program, I'm using the exec() method (similar to Java). It successfully executed the command and created the graph when I used the below code snippet:
var cmd: String = "dot -Tpng Graph.dot -o Graph.png"
var run: Runtime = Runtime.getRuntime() ;
var pr: Process = run.exec(cmd) ;
However It fails to execute after changing the path of the input and output files (I just included a directory inside which the input file and output file resides as shown below)
def main(args: Array[String]): Unit = {
var DirectoryName: String = "Logs"
var GraphFileName: String = DirectoryName + File.separator + "Graph.dot"
val GraphFileObj: File = new File(GraphFileName)
// var cmd: String = "dot -Tpng Graph.dot -o Graph.png"
var cmd: String = "dot -Tpng \"" + GraphFileObj.getAbsolutePath + "\" -o \"" + DirectoryName + File.separator + "Graph.png\"" ;
println(cmd)
var run: Runtime = Runtime.getRuntime() ;
var pr: Process = run.exec(cmd) ;
}
The same command when executed through terminal gives proper output. Can you please help me to find what I'm missing?
exec is not a shell...e.g. quoting won't work as you expect, and thus your path (which may contain spaces, etc) will not be processed as you expect. The command will be broken apart using StringTokenizer, and your literal quotes will be...well..literal.
Use the form of exec that takes an array instead, so you can tokenize the command correctly.
val args = Array[String]("dot", "-Tpng", GraphFileObj.getAbsolutePath, ...);
run.exec(args)