Calling java command line applications - java

So I have the following command that runs java application from the command line and produces a bunch of output.
java -jar client.jar --server test7.contoso.com --alternativeemailaddress fred1#contoso.com --organisation contoso --emailaddress fred#contoso.com --data1 1234 --Password1 123456 --data2 1234 --Password2 1234
I'm trying to run this from within PowerShell and capture the output to a variable for further processing.
So far I've found 100's of different ways to do this, but none have really worked particularly well.
Is there any best practice/convention as to how to get this done?

See: https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters/
For many ways to execute "legacy" commands with PowerShell
You need to include the .exe for one.
The new V3+ recommended way is to use --% to tell PowerShell to not parse the remaining arguments
PS> java.exe --% -jar client.jar --server test7.contoso.com --alternativeemailaddress fred1#contoso.com --organisation contoso --emailaddress fred#contoso.com --data1 1234 --Password1 123456 --data2 1234 --Password2 1234

OK so I worked it out. This is what I went with.
$params = #{ 'jar'='C:\client\client.jar';
'param1'='data1'
'param2'='data2'
'param3'='data3'
'param4'='data4'
'param5'='data5'
'param6'='data6'
'param7'='data7'
'param8'='data8'
'param9'='data9'
'param10'='data10'
'error_log'='C:\tmp\error_log.txt'
}
$data = & 'C:\Program Files\java\jdk1.8.0_121\bin\java.exe' -jar $params.'jar' `
--param1 $params.param1 `
--param2 $params.param2 `
--param3 $params.param3 `
--param4 $params.param4 `
--param5 $params.param5 `
--param6 $params.param6 `
--param7 $params.param7 `
--param8 $params.param8 `
--param9 $params.param9 `
--param10 $params.param10 `
2>$params.error_log

Related

Why is ANTLR not printing set of tokens correctly?

I am testing to see if ANTLR-4.7.1 is working properly by using a sample, provided by my professor, to match these results for the same printed set of tokens:
% java -jar ./antlr-4.7.1-complete.jar HelloExample.g4
% javac -cp antlr-4.7.1-complete.jar HelloExample*.java
% java -cp .:antlr-4.7.1-complete.jar org.antlr.v4.gui.TestRig HelloExample greeting helloworld.greeting -tokens
[#0,0:4='Hello',<1>,1:0]
[#1,6:10='World',<3>,1:6]
[#2,12:12='!',<2>,1:12]
[#3,14:13='<EOF>',<-1>,2:0]
(greeting Hello World !)
However, after getting to the 3rd command, my output was instead:
[#0,0:4='Hello',<'Hello'>,1:0]
[#1,6:10='World',<Name>,1:6]
[#2,12:12='!',<'!'>,1:12]
[#3,13:12='<EOF>',<EOF>,1:13]
In my output, there are no numbers inside < >, which I believe should be defined from the HelloExample.tokens file that contain:
Hello=1
Bang=2
Name=3
WS=4
'Hello'=1
'!'=2
I get no error information and antlr seemed to have generated all the files I needed, so I don't know where I should be looking to resolve this, please help. And I'm not sure if it'll be of use, but my working directory started with helloworld.greeting and HelloExample.g4 and final directory now contains
helloworld.greeting
HelloExample.g4
HelloExample.interp
HelloExample.tokens
HelloExampleBaseListener.class
HelloExampleBaseListener.java
HelloExampleLexer.class
HelloExampleLexer.inerp
HelloExampleLexer.java
HelloExampleLexer.tokens
HelloExampleListener.class
HelloExampleListener.java
HelloExampleParser$GreetingContext.class
HelloExampleParser.class
HelloExampleParser.java
As rici already pointed out in the comments, getting the actual rule names instead of their numbers in the token output is a feature and shouldn't worry you.
In order to get the (greeting Hello World !) output at the end, you'll want to add the -tree flag after -tokens.

Can't Start Minecraft with Command Line

I tried to start minecraft with these args:
arguments = ["javaw.exe", "-Xmx"+str(max_mem), "-XX:+UseConcMarkSweepGC", "-XX:+CMSIncrementalMode",
"-XX:-UseAdaptiveSizePolicy", "-Xmn"+str(min_mem),
"-XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump",
"-Djava.library.path="+natives_dir, "-Dminecraft.launcher.brand=java-minecraft-launcher",
"-Dminecraft.launcher.version=1.6.89-j",
"-cp "+libraries,
"net.minecraft.client.main.Main", "--username "+player, "--version "+MINECRAFT_VERSION,
"--accessToken 0", "--userProperties {}", "--gameDir "+CLIENT_PATH, "--assetsDir "+assets_dir,
"--assetIndex ", "--width "+width, "--height "+height]
However, I receive this error:
Unrecognized option: -cp C:/Users/danch/AppData/Roaming/.launcor/libraries/ca/weblite/java-objc-bridge/1.0.0/java-objc-bridge-1.0.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/google/guava/guava/21.0/guava-21.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/ibm/icu/icu4j-core-mojang/51.2/icu4j-core-mojang-51.2.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/mojang/authlib/1.5.25/authlib-1.5.25.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/mojang/patchy/1.1/patchy-1.1.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/mojang/realms/1.10.22/realms-1.10.22.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/mojang/text2speech/1.10.3/text2speech-1.10.3.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/paulscode/codecjorbis/20101023/codecjorbis-20101023.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/paulscode/codecwav/20101023/codecwav-20101023.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/paulscode/libraryjavasound/20101123/libraryjavasound-20101123.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/paulscode/librarylwjglopenal/20100824/librarylwjglopenal-20100824.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/com/paulscode/soundsystem/20120107/soundsystem-20120107.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/commons-codec/commons-codec/1.10/commons-codec-1.10.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/commons-io/commons-io/2.5/commons-io-2.5.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/io/netty/netty-all/4.1.9.Final/netty-all-4.1.9.Final.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/it/unimi/dsi/fastutil/7.1.0/fastutil-7.1.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/net/java/dev/jna/jna/4.4.0/jna-4.4.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/net/java/dev/jna/platform/3.4.0/platform-3.4.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/net/java/jinput/jinput/2.0.5/jinput-2.0.5.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/net/java/jinput/jinput-platform/2.0.5/jinput-platform-2.0.5-natives-windows.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/net/java/jutils/jutils/1.0.0/jutils-1.0.0.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/net/sf/jopt-simple/jopt-simple/5.0.3/jopt-simple-5.0.3.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/apache/httpcomponents/httpclient/4.3.3/httpclient-4.3.3.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/apache/logging/log4j/log4j-api/2.8.1/log4j-api-2.8.1.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/apache/logging/log4j/log4j-core/2.8.1/log4j-core-2.8.1.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/lwjgl/lwjgl/lwjgl/2.9.2-nightly-20140822/lwjgl-2.9.2-nightly-20140822.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/lwjgl/lwjgl/lwjgl/2.9.4-nightly-20150209/lwjgl-2.9.4-nightly-20150209.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/lwjgl/lwjgl/lwjgl-platform/2.9.2-nightly-20140822/lwjgl-platform-2.9.2-nightly-20140822-natives-windows.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/lwjgl/lwjgl/lwjgl-platform/2.9.4-nightly-20150209/lwjgl-platform-2.9.4-nightly-20150209.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/lwjgl/lwjgl/lwjgl_util/2.9.2-nightly-20140822/lwjgl_util-2.9.2-nightly-20140822.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/org/lwjgl/lwjgl/lwjgl_util/2.9.4-nightly-20150209/lwjgl_util-2.9.4-nightly-20150209.jar;C:/Users/danch/AppData/Roaming/.launcor/libraries/oshi-project/oshi-core/1.1/oshi-core-1.1.jar;C:/Users/danch/AppData/Roaming/.launcor/versions/1.12.2/1.12.2.jar
What did I do wrong?
It's not
"-cp "+libraries,
it is
"-cp",
libraries,
Everywhere you would type a space on the command line is a new argument! So you have to change this in multiple places.
Maybe try -classpath instead of -cp.
Not sure if that's because of your Java version or what.

Get specific java version with powershell

I have some issues with getting the java version out as a string.
In a batch script I have done it like this:
for /f tokens^=2-5^ delims^=.-_^" %%j in ('%EXTRACTPATH%\Java\jdk_extract\bin\java -fullversion 2^>^&1') do set "JAVAVER=%%j.%%k.%%l_%%m"
The output is: 1.8.0_121
Now I want to do this for PowerShell, but my output is: 1.8.0_12, I miss one "1" in the end Now I have tried it with trim and split but nothing gives me the right output can someone help me out?
This is what I've got so var with PowerShell
$javaVersion = (& $extractPath\Java\jdk_extract\bin\java.exe -fullversion 2>&1)
$javaVersion = "$javaVersion".Trim("java full version """).TrimEnd("-b13")
The full output is: java full version "1.8.0_121-b13"
TrimEnd() works a little different, than you might expect:
'1.8.0_191-b12'.TrimEnd('-b12')
results in: 1.8.0_19 and so does:
'1.8.0_191-b12'.TrimEnd('1-b2')
The reason is, that TrimEnd() removes a trailing set of characters, not a substring. So .TrimEnd('-b12') means: remove all occurrences of any character of the set '-b12' from the end of the string. And that includes the last '1' before the '-'.
A better solution in your case would be -replace:
'java full version "1.8.0_191-b12"' -replace 'java full version "(.+)-b\d+"','$1'
Use a regular expression for matching and extracting the version number:
$javaVersion = if (& java -fullversion 2>&1) -match '\d+\.\d+\.\d+_\d+') {
$matches[0]
}
or
$javaVersion = (& java -fullversion 2>&1 | Select-String '\d+\.\d+\.\d+_\d+').Matches[0].Groups[0].Value

Getting no such file error when trying to run Maven wrapper? [duplicate]

I am trying to format a variable in linux
str="Initial Value = 168"
echo "New Value=$(echo $str| cut -d '=' -f2);">>test.txt
I am expecting the following output
Value = 168;
But instead get
Value = 168 ^M;
Don't edit your bash script on DOS or Windows. You can run dos2unix on the bash script. The issue is that Windows uses "\r\n" as a line separator, Linux uses "\n". You can also manually remove the "\r" characters in an editor on Linux.
str="Initial Value = 168"
newstr="${str##* }"
echo "$newstr" # 168
pattern matching is the way to go.
Try this:
#! /bin/bash
str="Initial Value = 168"
awk '{print $2"="$4}' <<< $str > test.txt
Output:
cat test.txt
Value=168
I've got comment saying that it doesn't address ^M, I actually does:
echo -e 'Initial Value = 168 \r' | cat -A
Initial Value = 168 ^M$
After awk:
echo -e 'Initial Value = 168 \r' | awk '{print $2"="$4}' | cat -A
Value=168$
First off, always quote your variables.
#!/bin/bash
str="Initial Value = 168"
echo "New Value=$(echo "$str" | cut -d '=' -f2);"
For me, this results in the output:
New Value= 168;
If you're getting a carriage return between the digits and the semicolon, then something may be wrong with your echo, or perhaps your input data is not what you think it is. Perhaps you're editing your script on a Windows machine and copying it back, and your variable assignment is getting DOS-style newlines. From the information you've provided in your question, I can't tell.
At any rate I wouldn't do things this way. I'd use printf.
#!/bin/bash
str="Initial Value = 168"
value=${str##*=}
printf "New Value=%d;\n" "$value"
The output of printf is predictable, and it handily strips off gunk like whitespace when you don't want it.
Note the replacement of your cut. The functionality of bash built-ins is documented in the Bash man page under "Parameter Expansion", if you want to look it up. The replacement I've included here is not precisely the same functionality as what you've got in your question, but is functionally equivalent for the sample data you've provided.

Why do I see scrabbled output when using JSch?

I am trying to use JSch. I tried the example here
Although I can connect the output is weird.
I get the following:
Last login: Thu Jan 31 19:44:25 2013 from 10.2.251.77
[1mcli:~ # [m
And if I do e.g. an ls I get:
[0m[01;34m.InstallAnywhere[0m [00m.bash_history [00m.bash_profile[0m
[01;34mbin[0m [00msles11-patched[0m
[01;34m.kbd[0m [00mindex.html[0m [00mtest.sql[0m
[00m.viminfo[0m [00;31mipvsadm-1.26-1.src.rpm[0m
[m[1mcli:~ # [m
These are the directory contents but why are they displayed like that?
I am running in this from Eclipse and this is what I see in Eclipse output. If I run this from Windows CMD it stucks
Update:
I noticed that if I connect to a different linux the output is fine!
Only if I connect to a specific linux installation I see these weird characters! Any idea what is causing this?
Update2:
Following the link of #PeterMmm I did printf "äöü" | xxd. Both the "bad" and good one give:
0000000: e4f6 fc
I also did locale.
In the "bad" case:
# locale
LANG=POSIX
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
In the good system:
LANG=POSIX
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
Configuration seems to be the same. So what could be causing this?
Please check
Funny Shell Output: [01;32mtestfile.txt[00m instead of testfile.txt
((ChannelShell) channel).setPtyType("dumb");
Does the trick.
They are escape sequences for the terminal emulation. I guess that there is no relation to the character encoding.
Update:
If ChannelShell#setPty(false) is invoked, a pseudo-terminal will not be allocated and escape sequences will not be appeared.
Channel channel=session.openChannel("shell");
((ChannelShell)channel).setPty(false); // !!
...
channel.connect();

Categories

Resources