I'm attempting to execute Cygwin commands on Windows from a Java application. In cygwin's bin, I noticed some files are Application type (.exe) while others (like zcat and zless) have no extension and are just File type.
I've added the bin to the Windows PATH and seem to only be able to execute the .exe files from cmd. The code below works.
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "ls");
Process p = pb.start();
I want to use things like zcat and zless but they aren't executable and cmd complains that 'zcat' is not recognized as an internal or external command,
operable program or batch file.
If I manually change the file to .exe, I get a pop-up error saying zcat can't start or run due to incompatibility with 64-bit versions of Windows. I've installed the 64-bit version of cygwin (setup-x86_64). Why isn't all of cygwin's bin executable?
Most of the cygwin programs are NOT binary program but script one.
The command file can give you a description of the file type:
$ file zcat
zcat: POSIX shell script, ASCII text executable
while
$ file cat
cat: PE32+ executable (console) x86-64, for MS Windows
reading the first 5 rows of zcat
$ head -n 5 zcat
#!/bin/sh
# Uncompress files to standard output.
# Copyright (C) 2007, 2010-2016 Free Software Foundation, Inc.
we see on first row the #! that says that is a script to be executed by the
/bin/sh interpreter.
In other case we can have
$ head -n5 2to3
#!/usr/bin/python2.7.exe
import sys
from lib2to3.main import main
sys.exit(main("lib2to3.fixes"))
so 2to3 is a python 2.7 script
Nobody else has said this, so for anybody else looking for this ... if you need to run the "z..." commands from the cmd window AND you have the cygwin bin directory in your path, you can run them (yea, it's a bit klugy) using bash, as in
bash zcat file.gz
etc.
Or you can run them directly from the cygwin terminal.
FWIW I had to find the 'more' utilities and load them. For some reason they weren't in my initial set of packages I downloaded
Related
When I have a jar(hoge.jar) file, and a unix shell script(hoge.sh) or a windows batch file(hoge.bat), I can obtain ones with the jar file like below:
cat hoge.sh hoge.jar >foo.sh
or
copy /b hoge.bat+hoge.jar foo.bat
Is there any way to do that with a powershell script (hoge.ps1)
copy /b hoge.ps1+hoge.jar foo.ps1
As for the foo.ps1 obtained as above,
java -jar foo.ps1
does work, but the part of the powerscript never runs... (when used as the powerscript ofcourse)
You can potentially store any file in a powershell script. Here is an excellent article that shows you how to do so with base 64 encoding.
I'm trying to execute a Java file I was given on Windows 10, inside of the Bash shell.
I open my command prompt. I enter bash.
I set
JAVA_CALL="C:/Program Files/Java/jdk1.8.0_192/jre/bin/java"
I try to execute the call, but to no luck. I read several threads on here and tried several things. I made sure my path includes both the Program Files x86 and the regular Program Files version of my JAVA.
I executed
sudo ln -s -f /mnt/c/Program\ Files/Java/jre1.8.0_192/jre/bin/java.exe /bin/java
To try and make a link to it. I cannot get it to wrong. It always tells me
-bash: C:/Program Files/Java/jdk1.8.0_192/jre/bin/java: No such file or directory
I am sure that file exists. Any ideas?
I want to run a jar file on a client machine using batch script. But the problem is its not clear where is the java installed. There are 3-4 fixed paths where java.exe can exist.
How to write a batch script to find where java is installed on any of those predetermined locations and then run a command to execute a jar file.
Assume java is not set in the environment variables and no access to windows registry.
On windows :
c:\> for %i in (java.exe) do #echo. %~$PATH:i
On linux:
$ which java
My java program was written on a windows machine and I am trying to get it installed and running on a Ubuntu 10.04 machine. I have created a .tar.gz file with myProgram.jar in it as well as 5 supporting library .jar files in a lib folder. Where do I put these files? Do I need to extract it on the Linux machine to a usr/bin folder? Does the shell script go inside the tar.gz? I have read that if you write the shell script on a windows machine you can have issues once you move it to the Linux machine, so I am writing the shell script on the Linux machine using gedit. I am just not sure what to do next.
So far in my script I have,
#!/bin/bash
java -jar myProgram.jar
I am going to try and extract the tar.gz file to the usr/bin directory and see if it runs.
Any suggestions or help would be greatly appreciated.
Thanks in advance,
Ray
Your question is quite "broad" :). I hope you find the following useful.
Do not extract the files to /usr/bin. See e.g. http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard on where and where not to put files on a *nix system.
Extract the jar's to e.g. /opt/yourProgram/*.
The shell script should be inside there too. Make sure its executable (i.e chmod 755 script.sh)
In your shell script add cd /opt/yourProgram to have the proper working directory for your program before you invoke java.
If you want this program to be started easily by everyone create a symbolic link in /usr/bin or better in /usr/local/bin pointing to your script. Do this as last step after everything else is working.
In your shell script you'll have to add the other jars to the classpath e.g.
java -cp lib/some.jar:lib/other.jar -jar myProgram.jar
or
java -cp lib/some.jar:lib/other.jar:myProgram.jar com.acme.ClassContainingMain
Recommended practice: Add set -e at the very beginning of your script
As you already mentioned it's considered harmful to edit a shell script using a windows editor. The reason is that the windows editor will encode line-breaks (i.e. you hit the Return key) differently. This will make bash puke :)
Im not too clear of what you are looking for.
The script that you have written should work absolutely fine if you have placed your script and myprogram.jar at the same level.
And also im not sure how your myprogram.jar is referring the dependent libraries. So can't comment on them. Best bet will be to place your script and all jars together and try running the script.
I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin