Expand wildcard in program's arguments - java

I have batch file which attempts to launch a java application:
java -jar myProgram*.jar
I would like the batch file to evaluate the wildcard * in order to find the program regardless of version numbers. So it should find myProgram1.jar, or myProgram438.jar and run it.
But this batch file yields:
Error: Unable to access jarfile myProgram*.jar
It looks like the arguments to java are not being processed by the shell. Is there a way to expand the wildcard in the arguments before passing them? I know that Bash has backtics which could do this. Is it possible to do in windows?

If there are more program*.jar in the folder you have to specify, which you want to start. The script starts the "last found":
#echo off&setlocal
for %%i in (myProgram*.jar) do set "jarProg=%%~i"
java -jar %jarProg%
You don't need "Cygwin" for this.

Related

How can I execute .jar file from the terminal without specifying its name

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"?

Translating windows bat file to linux shell script

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

How to run a exe file via unix by passing input parameter

I have created a exe file from jar via converter tools. Jar file was executing fine when I tried to run via unix by passing input parameters eg: java -jar SSS_Infinite.jar test.in 2
However after converting to exe I tried to run by passing input parameters via Unix but its not working and simply returns to the next line. I tried the below command in Unix cmd. Is there any other alternative to make it trigger ?
SSS_Infinite.exe test1.in 2
I assume you created executable for Windows platform, it will not work on *nix systems.
The simplest option will be to build little script that will accept parameters and pass them to java -jar, something like that:
#!/bin/bash
java -jar SSS_Infinite.jar $1 $2
where $1 and $2 are script arguments, see explanation here.
after you create that script and save it as say SSS_Infinite.sh, change its permissions:
chmod +x SSS_Infinite.sh
Then you'll be able to execute it like that:
./SSS_Infinite.sh test1.in 2

Rclick registry run java program

I run my java program in command linewith this statement
java myclass -a filetype.txt
But I want to run my program with right-click on a file with manipulating shell registry, but i don't what should write in shell default value
e.g for adding an option right-click to run with command prompt in windows, I set the default value with C:\Windows\System32\cmd.exe
but don't know how can run my java program with a simple right click.
If I understood you right, you want to right click on filetype.txt and run your java class on it?!
To achieve that you could do the following steps:
create a batch file (e.g. run.cmd) with the following content:
java -cp C:\path\to\myclass myclass -a %1
create a registry key bellow HKEY_CLASSES_ROOT\*\shell (or HKEY_CLASSES_ROOT\.txt\shell if you want to apply your prog only to txt files)
name it what you want, and give it a value of your choice. This value will be what you see in your context menu
create another key bellow the recently created one and name it command
give it the value C:\path\to\run.cmd %1
that did the trick for me. If you don't like the additional *.cmd file, put
cmd /c java -cp C:\path\to\myclass myclass -a %1
as the value for the command key. And remember to use double quotes for paths that contain whitespaces.

Setting lucene jar files in java classpath

I'm new to lucene and is having trouble getting started.
Following the beginners guide at http://lucene.apache.org/java/3_3_0/demo.html i'm trying to set the classpath, copying the syntax from http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html.
this is what I entered in the command line:
C:\Users\k>java -classpath C:\Users\k\Downloads\lucene-3.3.0\contrib\demo\lucene-demo-3.3.0.jar;C:\Users\k\Downloads\lucene-3.3.0\lucene-core-3.3.0.jar
It returns a list of options usable with the java keyword.
What am i doing wrong ?
You need something along the lines of
C:\Users\k>java -classpath C:\Users\k\Downloads\lucene-3.3.0\contrib\demo\lucene-demo-3.3.0.jar;C:\Users\k\Downloads\lucene-3.3.0\lucene-core-3.3.0.jar org.apache.lucene.demo.IndexFiles -docs {path-to-lucene}/src
It looks like you set the classpath correctly, all you needed to do after that was org.apache.lucene.demo.IndexFiles which tells the JVM which is the main class of the application and -docs {path-to-lucene}/src is an argument passed into the lucene demo.
The command you are using is not for setting class path. It is the java command used to run java class file. You are providing it a class path arguments which determines from where to load class files.
To set classpath use this command on windows:
set CLASSPATH=classpath1;classpath2...
So if you want to still use java command with -classpath argument then specify a class name at the end of command which is the class going to be run like
C:\Users\k>java -classpath C:\Users\k\Downloads\lucene-3.3.0\contrib\demo
\lucene-demo-3.3.0.jar;C:\Users\k\Downloads\lucene-3.3.0\
lucene-core-3.3.0.jar MyClassName

Categories

Resources