Running Java batch file on Mac - java

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.

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

Getting Error while running program on linux. On windows it is working fine

I made a java program. I used eclipse and it's a maven project. Now when i run the program from windows command prompt, then it's run fine. Here How i am running it from windows command prompt
D:\Personal Work\eclipse 32 Bit\workspace\....\target\classes>
java -cp ".;..\dependency-jars\*" com/softech/ls360/integration/BatchImport vintners
It is working fine. My dependency jar folder contains these jar files
Now when i run the same program from linux. Here how i am runnign it
root#Basit:/home/test/script/classes# java -cp .;../dependency-jars/*; com.s
oftech.ls360.integration.BatchImport vintners
Then i am getting the errors that
....
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
../dependency-jars/commons-collections-3.2.1.jar: line 1: PK??: command not found
../dependency-jars/commons-collections-3.2.1.jar: line 2:
../dependency-jars/commons-collections-3.2.1.jar: line 2: ?8: command not found
../dependency-jars/commons-collections-3.2.1.jar: line 3: syntax error near unex
pected token `)'
../dependency-jars/commons-collections-3.2.1.jar: line 3: ? ¶META-INF/MANIFE
ST.MF?VKo
_¦?z? ?%+v?N??!ö!P#
(
_?o.5?$
com.softech.ls360.integration.BatchImport: command not found
Why i am getting these errors. How can i run it on linux? Please help
Thanks
You need to use : instead of ; in classpath on linux envrionment. Assuming you have jars placed properly, then simply changing this:
java -cp .;../dependency-jars/*; com.s
oftech.ls360.integration.BatchImport vintners
to
java -cp .:../dependency-jars/*: com.s
oftech.ls360.integration.BatchImport vintners
should work
Learn more about setting classpath here: http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
The semicolon is making Bash invoke the java command with no classpath, then tries to execute each jar directly, seeking a shebang where none exists. This leads to JAR headers being printed as part of the error.
Use : to separate jars instead of semicolons on Linux.
You need to make two changes:
First, the classpath separator is ':' rather than ';' on Linux
Second, you'll need to escape the wildcard character with a backslash ('\'), or the shell will expand it and mess things up. You want Java to see the '*' character and expand it itself. The Windows shell doesn't expand wildcards on command lines, so this isn't a problem there.
So, all together, you'll want to use something like
java -cp .:../dependency-jars/\*: com.softech.ls360.integration.BatchImport vintners
You should use : instead of ; as a sperator of the class path files.

Opening Files with Java while Working in Cygwin

I am running Cygwin on a Windows 7 machine, and using script files to execute Java programs in batch. My problem is this: I try to pass in a Cygwin / Linux path to a file, via the command line, and Java converts all of the forward slashes to backslashes.
For instance:
java program $scratchname/path_to_folder/ filename_$i.txt
Within Java, I take the directory and add the file name to open the file, which usually works with no issues as long as I'm using a Windows command line. However, in Cygwin Java converts this to:
home\scratch\path_to_folder
which Cygwin doesn't like.
I don't think this is a simple matter of replacing the backslashes with forward slashes, because Java seems to default to the Windows path conventions when I try to open the file. I'm guessing this is because Cygwin is pointed to the Windows installation of the JVM.
How can I force Java to use Cygwin / Linux path name conventions on a Windows system?
Java is a Windows program, and as such, only understands Windows paths; launching it from a Cygwin shell can't change that. You can use cygpath to convert paths back and forth.
Reference link: https://cygwin.com/cygwin-ug-net/using-effectively.html
Example case:
java -jar path/to/your-1.0.jar "$(cygpath -aw /home/YOUR_USER/path/to/file.txt)"
Options:
a provides the absolute path
w uses the Windows format

Starting a java program from shellscript

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

Categories

Resources