setting multiple jar files as classpath in cygwin - java

I have the program x.java in c:\cygwin\programs\x.java and it uses y.jar and z.jar that are also in the folder c:\cygwin\programs.
In windows:
c:cygwin\programs>javac -classpath c:\cygwin\programs\y.jar;c:\cygwin\programs\z.jar x.java
No errors.
In cygwin
(1)
$javac -classpath c\:/cygwin/programs/y.jar;c\:/cygwin/programs/z.jar x.java
Errors: $'PK\003\004': Command not found.
(2)
$javac -classpath c:\cygwin\programs\y.jar;c:\cygwin\programs\z.jar x.java
Errors: -bash command Command not found.
(3)
$javac -classpath 'c:/cygwin/programs/y.jar;c:/cygwin/programs/z.jar' x.java
No error.
Why is it giving error in case of (1),(2)...

You are messing up with escape character back-slash \. In Unix based environment, it's better to use / as path separator. If you want to use backlashes for some reason, use an additonal backslash i.e. \\ to treat it as literal in the path.
Because of above, first tow statements are not resulting into correct path and hence failure.

Cygwin treats the ; character as starting a new command line, so in (1) it is trying to execute the separate commands
$ javac -classpath c\:/cygwin/programs/y.jar
$ c\:/cygwin/programs/z.jar x.java
The error message is from Cygwin trying to execute the jar file directly as a script.
You can quote the entire argument with '' as in (3), or escape the semicolon:
$ javac -classpath c\:/cygwin/programs/y.jar\;c\:/cygwin/programs/z.jar x.java

Related

How do I run a Command Line script from PowerShell? [duplicate]

I'm trying to run a java process via Powershell in Windows XP. Here's the command:
java.exe -cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install
So, the classpath is . and .\common.jar (I think java takes the wrong slashes, right?) There are two environment variables, one "atest1" the other "." and the class to execute main on is DW_Install (in the default package).
This command works in cmd.exe, but doesn't is PS. What's going on? What is PS doing while parsing this command that CMD doesn't do (or vice versa)?
Aaron
The problem is that PS for some reason parses -Dresourcepath=. differently than cmd. What works is
java -cp '.;.\common.jar' -Dcontext=atest1 "-Dresourcepath=." DW_Install
It doesn't matter which way the slash goes, and it doesn't matter which quotes one uses (' or "). The classpath must be escaped, however, with some kind of quotes. A good test to see what's getting by the PS interpreter is to echo it. The following:
echo java -cp '.;.\common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
yields the following output:
java
-cp
.;.\common.jar
-Dcontext=atest1
-Dresourcepath=
.
DW_Install
(Notice the resourcepath and the value of resourcepath are not on the same line.) Whereas the output to
echo java -cp '.;.\common.jar' -Dcontext=atest1 '-Dresourcepath=.' DW_Install
yields the following output:
java
-cp
.;.\common.jar
-Dcontext=etaste1
-Dresourcepath=.
DW_Install
Which is much more to our liking.
Although I wish this upon none of you, I hope that this post helps those of you that must deploy java projects on Windows machines (even though they will not run on any other platform ever).
Running external command-line programs from PowerShell is sometimes a bit problematic because there PowerShell exposes two different parsing modes that get trumped by the different syntaxes of said external programs.
In any case, running a command in Powershell requires using either the . prefix (dot-"sourcing") or the & operator.
You can workaround this by passing each parameter to the external program as separate variables, like so:
PS> $classpath = ".;./common.jar"
PS> $env = "-Dcontext=atest1 -Dresourcepath=."
PS> $class = "DW_Install"
PS> . java.exe -cp $classpath $env $class
Another example based on https://gaming.stackexchange.com/questions/24543/how-do-i-change-player-name-in-minecraft-multiplayer-in-offline-mode-in-linux
function mineCraftAs {
Param (
[parameter(mandatory=$true, HelpMessage="Minecraft character name." ,ValueFromPipeline=$true)]
[string] $name
)
if(!(test-path $env:appdata)) { $(throw "Appdata not found at $env:appdata")}
$private:minecraftPath=Join-Path $env:appdata .minecraft
if(!(test-path $minecraftPath)) { $(throw "Minecraft not found at $minecraftpath")}
$private:minebinPath=join-path $minecraftPath "bin"
if(!(test-path $minebinPath)) { $(throw "Minecraft bin not found at $minebinPath")}
$minebinPath | write-debug
gci $minebinpath | write-debug
#java -Xms512m -Xmx1024m -cp "%APPDATA%/.minecraft\bin\*" -Djava.library.path="%APPDATA%\.minecraft\bin\natives" net.minecraft.client.Minecraft '"'%1'"'
echo java -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
$minecraftJob=& 'C:\Program Files (x86)\Java\jre6\bin\java.exe' -Xms512m -Xmx1024m -cp ('"'+$minebinPath+'\*"') ('-Djava.library.path="'+$minebinPath+'\natives"') net.minecraft.client.Minecraft ($name)
}
minecraftas newbie
The following should work:
java.exe -cp '.;./common.jar' -Dcontext=atest1 -Dresourcepath=. DW_Install
I guess that PowerShell interprets the ; in the classpath as command delimiter, thereby trying to run java -cp . and ./common.jar -D....
start-process -nnw java "-cp .;./common.jar -Dcontext=atest1 -Dresourcepath=. DW_Install"

interactive java invocation is fine but the same in a script fails

My script is not running but the same command interactively seems to work. First I try a * wildcard and it fails with a "could not find or load" probably because I want the wildcard to get a Java interpretation, not the Linux interpretation. So I quote the wildcard and then it works: (Java gets the wildcard and interprets it I believe to mean "all the JAR files"). Next I try the same from a script and it fails.
root#server0:~/serverDeployment/application# java -enableassertions
-classpath export/ohana1/* info.zqxj.trader1.CommandProcessor help
Error: Could not find or load main class export.ohana1.commons-collections-3.2.1.jar
root#server0:~/serverDeployment/application# java -enableassertions
-classpath export/ohana1/"*" info.zqxj.trader1.CommandProcessor help
AssertionsEnabled
echoing arguments at the runtime local date 2016 09 06 local time 17:15:01.4...
help
END of echoed arguments.
CommandProcessor: Will process the >help< argument.
CommandProcessor: END of processing the >help< argument.
root#server0:~/serverDeployment/application# cat runner.sh
#!/bin/bash
CLASSPATH='-classpath export/ohana1/"*"'
echo "$CLASSPATH"
java -enableassertions "$CLASSPATH" info.zqxj.trader1.CommandProcessor help
exit 0
root#server0:~/serverDeployment/application# ./runner.sh
-classpath export/ohana1/"*"
Unrecognized option: -classpath export/ohana1/"*"
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
The option is -classpath but for some reason the entire -classpath export/ohana1/"*" is taken to be an option. Why is this happening?
Edit: Costi Ciudatu points out that I double quoted the string -classpath which is true it got double quoted on the line with the java invocation so I stopped doing that in this example that follows. However, it still fails.
root#server0:~/serverDeployment/application# cat runner.sh
#!/bin/bash
CLASSPATH='-classpath export/ohana1/"*"'
echo $CLASSPATH
java -enableassertions $CLASSPATH info.zqxj.trader1.CommandProcessor help
root#server0:~/serverDeployment/application# ./runner.sh
-classpath export/ohana1/"*"
Error: Could not find or load main class info.zqxj.trader1.CommandProcessor
BTW: The echo command seems to indicate that the double quote does no harm, both times I get the contents of the single quote. So echo "$VAR" seems to do the same as echo $VAR so perhaps there is no harm doing the same in the java invocation.
This happens because you put both the option and the value it in double quotes. Try something like:
#!/bin/bash
CLASSPATH='export/ohana1/*'
java -enableassertions -cp "$CLASSPATH" info.zqxj.trader1.CommandProcessor help

javac: invalid flag: activation-1.1.jar

I'm using Tomcat7 , jdk 1.7.0_55 & eclipse, when I trying to compile the entire project(Java Code) using COMMAND PROMPT, its showing Error Like
javac: invalid flag: D:\COMPILE\lib\activation-1.1.jar.
The given below steps are followed to compile the code.
Step.1: dir *.java /s /b > FilesList.txt
Step.2: javac #FilesList.txt -d compiledCode -cp D:\COMPILE\lib\*.jar
After run the Step.2 command its showing Error.so I removed the error jar file from my lib folder & run the command but its showing same error with another jar.
Note: I Already have ANT build.xml but I want to compile the project through COMMAND PROMPT.
The lib*.jar gets expanded by the command shell. You need to avoid that by using quotes.
***** -cp "D:\COMPILE\lib\*" *****
The argument to -cp is a single path list (like $PATH, not multiple arguments with one path each). Multiple files can be separated by : (or ; on Windows)

bash force double quotes in command

My bash script:
set -x
classpath=a:b
java -cp \"$classpath\" className
Basically I want the command java -cp "a:b" className to be executed on shell.
The output is :
"a.sh" 3L, 55C written
rajatk#localhost:~$ bash a.sh
+ classpath=a:b
+ java -cp '"a:b"' className
Error: Could not find or load main class className
rajatk#localhost:~$
Why is it adding ' before and after the ". How do I force remove it?
You don't need to escape the quotes in the original command.
set -x
classpath=a:b
java -cp "$classpath" className
The quotes are only there as part of bash's syntax (to treat the value of $classpath as a single word), not as part of the actual string passed as the argument to the -cp option.

Ubuntu: javac package gnu.io does not exist

I am running on Ubuntu 12.04. I am trying to communicate my Arduino with Java.
I have a source file SerialTest.java in directory /home/me/Desktop/folder
Inside the directory contains both RXTXcomm.jar, librxtxSerial.so, and my source code file.
I tried these methods to compile the SerialTest.java:
$ javac SerialTest.java
and I get the following errors:
SerialTest.java:4: error: package gnu.io does not exist
$ javac -verbose -classpath .:RTXXcomm.jar SerialTest.java
And I got the same error
SerialTest.java:4: error: package gnu.io does not exist
What I have referred to: javac -classpath not doing the trick
May I know what I am doing wrong? I am new to this. Thank you.
Your classpath option is wrong. It should be:
-classpath .:RTXXcomm.jar
Note the colon (:). The semicolon (;) is used as a command separator by the shell.
What happens currently is that this command is executed:
javac -verbose -classpath .
Hence the first error: "no source files". And then this command is passed:
RTXXcomm.jar SerialTest.java
Hence the second error: "RXTXcomm.jar: command not found."

Categories