I have a program where I have to take paths from the command line as input, likely with wildcards such as ?, * and **. Java persists in expanding these paths on it's own, usually screwing up.
For instance:
java -jar myapp.jar C:/Hello/World/*
Gives me a String[] args that looks like this:
["C:\Hello\World\foo", "C:\Hello\World\bar"]
Not only is this extremely troublesome, it also messes up the ** wildcard:
java -jar myapp.jar C:/Hello/World/**
Gives the same thing as before, when ** should be a recursive search. I have an algorithm for doing this efficiently, which I originally developed on python, but it's pointless if I can't use it. Is there a way to prevent this from happening?
EDIT = I tried using different shells (PowerShell, cmd, eclipse), using different formats (no quotes, quotes, double quotes) and none of them worked.
Java is not doing that, it's your shell.
Quote the arguments:
java -jar myapp.jar 'C:/Hello/World/**'
Or
java -jar myapp.jar "C:/Hello/World/**"
This could be completely wrong, but try this:
java -jar myapp.jar C:/Hello/World/\*
Related
How can I execute .jar file in folder from the Windows cmd without specifying its name.
I have tried below command (as there is only 1 jar in that folder I used *) but its not working(Error: Unable to access jarfile *.jar
).
java -jar *.jar
I am not sure it would be a good idea to just run everything in a directory, but you could:
FOR %A IN ("*.jar") DO (java -jar "%~A")
So what you appear to be asking is how to run the command
% java -jar somelongname.jar
as
% java -jar *.jar
to avoid some typing. That's not possible, because neither the Windows CMD shell or the java command is going to expand the *.jar wildcard pattern.
(On Linux / Unix / MacOS, the shell does wildcard expansion before passing arguments to a command. On Windows, it is the responsibility of the command to do this. In practice, it appears that the java command only expands wildcards in the arguments that are going to be passed to your application; see Stop expanding wildcard symbols in command line arguments to Java)
So if you want to avoid typing those pesky characters on Windows, you will need to do something like:
write a simple BAT file to run "java -jar somelongname.jar", or
write a clever BAT file to identify and run a JAR file that matches "*.jar", or
use Powershell.
For what it is worth, I think what you are trying to do is rather dangerous. This is a bit like typing "di*" to run the "dir". What if there is some other more dangerous command on the PATH that is going to match instead of "dir"?
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 am trying to run a Java application on a Mac. Currently, there is a batch file that works for Windows that looks like this:
java -cp lib/appframework-1.0.3.jar;lib/commons-net-3.1.jar;lib/mysql-connector-java-5.1.6-bin.jar;lib/swing-worker-1.1.jar;TimeCardApplicationOdesk.jar org.ep.gui.TimeCardApplication
This works fine on Windows, but running that command on a Mac outputs a bunch of gibberish (mostly "command not found" errors").
I have tried to set the class path beforehand and load the jars with the -jar switch, but I'm stumped and know nothing about Java. I'm sure there are some slight changes that need to be made to the syntax, but I'm lost.
Replace all semicolons (;) with colons (:) in the command.
Multiple path entries to the -cp flag are separated by colons on unix systems. Following examples from these docs (solaris and windows) illustrate this.
On a windows system:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses ...
Note that the two paths are separated by a semicolon.
And on a unix like system:
% java -classpath /java/MyClasses:/java/OtherClasses ...
Note that the two paths are separated by a colon.
I'm complete Linux newbie, but still want to provide a simple way for Linux users to start my Java program.
Therefore I want to create a shellscript.
I can't test my script so I'll have to ask here if this is working correctly:
#!/bin/bash
java -cp "bin";"extres/junit.jar" data.ProgramOne
exit 0
Your mistake is in path delimiter. It is ; on Windows and : on Linux.
Moreover you should not wrap each classpath fragment with "". On unix you can escape spaces and other forbidden characters using \. So, I'd re-write the java execution line as:
java -cp bin:extres/junit.jar data.ProgramOne
This will run when you are executing script from your app directory where you have subdirectory bin and extres.
try this:
java -cp "bin:extres/junit.jar" data.ProgramOne
Java under Unixes uses : as the separator in the classpath, so you'd need (the quotes are not necessary):
#!/bin/bash
java -cp bin:extres/junit.jar data.ProgramOne
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.