Perl Escape Quotes Problem - java

Pardon me if this is too trivial.
I am doing an XSLT by calling system() in Perl script like this:
system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile $inFile $xslFile $saxonParams");
$inFile is a string that contains a relative path to the xml file that is going to be translated using XSLT.This worked fine except for those $inFile that has space in the string, for example, like "Intro to Dance.htm", then it will report a syntax error.
If this is in MS-DOS, then I can easiliy get around this problem by putting a quote around the $inFile string in the XSLT command. I did try putting escape in the above command like:
system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile \"$inFile\" $xslFile $saxonParams");
It does not work. Could anybody help how should I put quotes around $inFile?
Thanks.

You can avoid the shell (and thus shell-escaping problems) by passing your command line to system as a list instead. Try
system( 'java', '-Xms256m', '-Xmx512m', '-jar', $saxonJar, '-o', $tmpFile, $inFile, $xslFile, $saxonParams );
See the perldoc for system for more on how this works.

Is this, in fact, MS-DOS?
You said it didn't work, but not what actually happened. Did you get an error? What was it?
If this is on a Unixish system, just substitute single quotation marks:
system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile '$inFile' $xslFile $saxonParams");
I don't know if that will work on MS-DOS or not.
If the filename contains " or ' it becomes a little more complex, because those will need to be preserved at the shell level:
$inFile =~ s/(['"])/\\$1/g;
system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile \"$inFile\" $xslFile $saxonParams");
(Or something similar.)
Better still, use the multi-argument form:
system('java', '-Xms256m', '-Xmx512m', '-jar', $saxonJar, '-o', $tmpFile,
$inFile, $xslFile, $saxonParams);
and let the interpreter and the shell figure it out.

Related

Wildcard in Jar name

I am running one jdk command in window batch file as:
javaw -Xms256M -Xmx1024M -Dspring.profiles.active=local -Dport=9001 -jar C:\Users\sampleJAR\myProj-1.0.0.jar
But, every 2 weeks we will have new version coming up and old jar will be replaced by new jar automatically so I was thinking to use wildcard with something like:
javaw -Xms256M -Xmx1024M -Dspring.profiles.active=local -Dport=9001 -jar C:\Users\sampleJAR\myProj-*.jar
I referred lots of articles online which suggested to use *, surround jar name with "" when using *,... None of them worked.
I believe that the articles you are looking at are in reference to the classpath option wildcard expansion.
The -jar option doesn't do this wildcard expansion and expects a filename without any wildcards.
You could try specifying the classpath with the wildcard and then putting the class name that you want to run at the end of the command. Hopefully like:
javaw -Xms256M -Xmx1024M -Dspring.profiles.active=local -Dport=9001 -cp "C:\Users\sampleJAR\*" com.my.classname

Resolve CLASSPATH with two JARs

I have a java app that needs two jar files to run. craftbukkit.jar is the one that holds the main function, and commons-dbcp-1.4.jar is what I need to allow mysql pooling. I am having issues getting the CLASSPATH to behave properly.
Can someone help point out what I am doing wrong here?
java -Xincgc -Xmx1G -cp "craftbukkit.jar;commons-dbcp-1.4.jar" org.bukkit.craftbukkit.Main nogui
Can't seem to find the Main when i do this, and without the commonds-dbcp-1.4.jar it fails to load properly.
Use java -Xincgc -Xmx1G -cp craftbukkit.jar:commons-dbcp-1.4.jar org.bukkit.craftbukkit.Main nogui
No quotes, and use :, not ;.
Add the line
Class-Path: commons-dbcp-1.4.jar
to Manifest.mf and make sure you leave an empty line at the end of the file assuming that commons-dbcp-1.4.jar is in the same directory.
Check your "path separator". Wich OS you are running on?
For Windows, path separator is ";". On Linux you should use ":"
Windows:
java -Xincgc -Xmx1G -cp "craftbukkit.jar;commons-dbcp-1.4.jar" org.bukkit.craftbukkit.Main nogui
Linux:
java -Xincgc -Xmx1G -cp "craftbukkit.jar:commons-dbcp-1.4.jar" org.bukkit.craftbukkit.Main nogui

Error: could not find or load main class (another one)

I've searched throughout this site and tried a few solutions when receiving this message but nothing seems to work.
I am trying to invoke a shell script on Ubuntu 12.04.2 (with java-7-openjdk-amd64) that runs a java program and then I get a "Error: Could not find or load main class com.xx" error.
This is how my script invokes Java:
"$JAVA" $server_jvmargs $javaProps -Dxx.home="$XX_HOME" -Duser.dir="$XX_HOME" -cp $client_classpath $mainclass $args
And the arguments you see above are defined as follows:
args=$*
javaProps=
mainclass=com.xx
server_jvmargs="-Djava.awt.headless=true -Xms1024m -Xmx1024m $jvmargs"
XX_HOME="`pwd`/../.."
client_classpath="$XX_HOME/lib/client/patch.jar;$XX_HOME/lib/client/xyx-xxx.jar;$clientlibs;$XX_HOME/lib/server/standard-1.1.2.jar;$publictilesource;$respath;$XX_HOME/lib/client/xxmainclass.jar"
The mainclass variable is in the classpath located in the xxmainclass.jar file so I'm not sure as to why it cannot find it?
Does anyone have any ideas on what could be going on?
To see what actually happens when you run your script, invoke it with bash -x, or put set -x at the top; this will print each command before it's run, so you can see how it's actually starting the JVM. Without this information, it's hard to come up with a better diagnosis. That said...
You've been copying off Tomcat's startup scripts, it looks like. Don't; they're awful.
Something a little more correct on the shell side might look like this:
args=( "$#" )
javaProps=( )
mainclass=com.xx
server_jvmargs=( -Djava.awt.headless=true -Xms1024m -Xmx1024m "${jvmargs[#]}" )
XX_HOME="$PWD/../.."
client_classpath="$XX_HOME/lib/client/patch.jar:$XX_HOME/lib/client/xyx-xxx.jar:$clientlibs:$XX_HOME/lib/server/standard-1.1.2.jar:$publictilesource:$respath:$XX_HOME/lib/client/xxmainclass.jar"
java \
"${server_jvmargs[#]}" \
"${javaProps[#]}" \
-Dxx.home="$XX_HOME" \
-Duser.dir="$XX_HOME" \
-cp "$client_classpath" \
"$mainclass" "${args[#]}"
The use of ${foo[#]} expands the array foo with literal contents. Note that foo must be created as an array in this case, and you need to be using a shell that supports arrays (so your script needs to start with #!/bin/bash, not #!/bin/sh).
See http://mywiki.wooledge.org/BashFAQ/005 for an introduction to arrays in bash.
use a : instead of a ; in your classpath.
unix just rolls that way.
Try this:
Java -jar pathToYOurFile.jar
Please check if line end character is OS specific in your shell script

How to run Java from Cygwin

I'm trying to write a BASH script to get my Java program to run(common issue, right?). I just can't quite get it to work. After many edits, here's how I am trying to set the classpath and then execute the program:
java -classpath 'cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar:/cygdrive
/c/Projects/common/lib/jdom-1.0.jar:/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar:
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar:/cygdrive/c/Projects
/Freereader/bin/"' com.free.syndication.SQLfeeder
Sorry the the jumble, I'm just trying to do everything at once. It tells me that the main class of my program cannot be found!((
Any ideas?
Java classpath uses semicolon as the token separator.
Use backticks instead of single quotes
Try:
java -classpath `cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar;/cygdrive
/c/Projects/common/lib/jdom-1.0.jar;/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar;
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar;/cygdrive/c/Projects
/Freereader/bin/"` com.free.syndication.SQLfeeder
In bash, the syntax $(command) is clearer than the backticks `command`
cygpath has a -p option to convert PATH-like values (as opposed to single path names) between Windows and Unix, i.e.
cygpath -pu 'C:\Users\me\bin;C:\Users\me\project\bin' will give /cygdrive/c/Users/me/bin:/cygdrive/c/Users/me/project/bin
cygpath -pw will do the same in the opposite direction
Note that cygpath -u "/cygdrive/c" (as in your question) will not change anything, since the directory name is already in the desired (Unix) syntax. You could omit it just as well.
So, the command becomes:
CP="C:/Projects/common/lib/rome-1.0.jar;C:/Projects/common/lib/jdom-1.0.jar;C:/Projects/common/lib/jsoup-1.6.1.jar;
C:/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar;C:/Projects
/Freereader/bin"
# for a Windows Java binary:
java -classpath "$(cygpath -pw "$CP")" com.free.syndication.SQLfeeder
# for a Unix Java binary:
java -classpath "$(cygpath -pu "$CP")" com.free.syndication.SQLfeeder
Alternatively, you can start with a Unix-style class path, but the commands stay the same. In either case, you can of course omit the call to cygpath if the class path is already in the desired syntax.
Don't you need backticks?
java -classpath `cygpath -u "/cygdrive/c/Projects/common/lib/rome-1.0.jar:/cygdrive
/c/Projects/common/lib/jdom-1.0.jar:/cygdrive/c/Projects/common/lib/jsoup-1.6.1.jar:
/cygdrive/c/Projects/common/lib/mysql-connector-java-5.1.18-bin.jar:/cygdrive/c/Projects
/Freereader/bin/"` com.free.syndication.SQLfeeder
You must use backticks ( '`' symbol ) or $(cmd) bash sytax to substitute cmd output
java do not understand unix- (cygwin-) style paths, only windows-style.
And at last first link in google answers you question
The main cause of the issue is NOT the backtic but instead the issue of colon versus semi-colon. Since in cygwin, the java running there is for DOS/Windows environment it is expecting ';' as the path separator.
While backtic does help, the main root cause of the issue must be emphasize the difference between ':' and ';' when Java is in Unix or in Windows environment.

Passing arguments to java vm from NSIS script

I'm developing my first java application using Eclipse. I've recently needed to adjust the amount of memory allocated by passing -Xmx256M to the JVM. The application is currently package up as a runnable jar and installed using the NSIS.
I'm having a problem passing arguments to the jar file once its installed. What is the common practice for doing this? Here is what I'm currently doing in my nsi file:
CreateShortcut "$SMPROGRAMS\$StartMenuGroup\$(^Name).lnk" "$SYSDIR\javaw.exe" "-Xmx256M -jar $INSTDIR\Foo.jar"
This results in the following being created as the shortcut Target on windows:
C:\WINDOWS\system32\javaw.exe -Xmx256M -jar C:\Program Files\Foo\Foo.jar
Unfortunately this does not work due to the space in C:\Program Files, If I change the link created manually to include quotes all is well:
C:\WINDOWS\system32\javaw.exe -Xmx256M -jar "C:\Program Files\Foo\Foo.jar"
UPDATE: Ordering of -jar and -Xmx256M swapped. The issue remains the same however. The spaces in the path to the jar file are causing an issue. I think I either need to find a way of adding quotes into the command, as shown when I manually change the target, or change my approach completely!
NSIS strings can be quoted with single quotes, double quotes, or the backward single quote. You can also escape with $\ ($\" etc)
CreateShortcut "$SMPROGRAMS\$StartMenuGroup\$(^Name).lnk" '"$SYSDIR\javaw.exe"' '-Xmx256M -jar "$INSTDIR\Foo.jar"'
Have you tried keeping the quotes in but escaping the path separators?
C:\WINDOWS\system32\javaw.exe -Xmx256M -jar "C:\\Program Files\\Foo\\Foo.jar"
Pretty sure you should put quotes around "C:\WINDOWS\system32\javaw.exe" even though there are no spaces.

Categories

Resources