I ran a java executable file using the following command
java -cp .;aa/bb/cc/dd/Main.jar aa.bb.cc.dd.Main
where aa.bb.cc.dd is the package that has all the .java, .class, and Main.jar files
and this package is inside the src folder from where I am running this command.
I am getting an error
cannot execute binary file: Exec format error
Can anyone tell me where I am going wrong.
Semi-colon in bash is used to separate commands. So in your case
java -cp .;aa/bb/cc/dd/Main.jar aa.bb.cc.dd.Main
java -cp . and aa/bb/cc/dd/Main.jar aa.bb.cc.dd.Main are considered as two separate commands and executed in sequence.
The issue can be fixed by changing the ";" to ":".
The path delimiter for java class path is ":" and not ";".
Related
I am trying to run .jar file for my java code from a .sh shell script file. the jar file name contains "." which is making the Cygwin terminal think it is a directory. Here is the command and the results:
java -jar ./lib/javax.json-1.0.jar
Result:
no main manifest attribute, in lib\javax.json-1.0.jar
Then:
error: package javax.json does not exist
import javax.json.Json;
With this mark ^ below the period (right after javax).
How can I solve it? I am working on Windows 10. Thanks!
EDIT:
I have written many forms of the .sh file to get it run, but it won't run. The current one is:
# !bin/bash
java -jar ./lib/javax.json-1.0.jar
java -jar ./lib/javax.json-api-1.0.jar
javac ./src/TimeTester.java
java TimeTester
Does this look good?
I am getting the following error:
.\src\TimeTester.java:22: error: package javax.json does not exist
import javax.json.Json; (With this ^ below the '.')
AND:
.\src\TimeTester.java:159: error: cannot find symbol
private static JsonObject getJsonFromString(String jsonStr){
And many similar lines in the error.. Any help?
EDIT 2:
This is my current file:
javac -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar ./src/TimeTester.java
java -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar:./src TimeTester
But I am getting:
.\src\TimeTester.java:22: error: package javax.json does not exist
import javax.json.Json;
^
With With this (^) under the last dot (.Json)
EDIT 3:
The current .sh file is:
#!/usr/bin/env bash
cd src
javac -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester.java
java -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester
The first command (javac) works and generates the .class file. BUT, the second command (java) does not work and it gives the following error:
Error: Could not find or load main class TimeTester
Your help is really appreciated!
Final EDIT:
Thanks for Jim, the shell script now works. Now I got a java execution error:
java.io.FileNotFoundException: .\in_input\in.txt (The system cannot find the path specified)
Thanks
TL;DR It is a pain to use Cygwin with programs written for Windows because of the conflicting command-line shell conventions between bash and cmd.exe. To compile and run Java programs it is much better to use an IDE such as Eclipse or Netbeans.
However, if you must...
None of this works because you are trying to pass Linux-style paths to the Windows JVM. However you seem to have a more basic misunderstanding:
# !bin/bash
java -jar ./lib/javax.json-1.0.jar
java -jar ./lib/javax.json-api-1.0.jar
javac ./src/TimeTester.java
java TimeTester
I am surmising that you think the first two statements make the libraries available to the compiler for the third javac line. This is not true, those two lines attempt to execute the jar file, which of course fails since the jar does not contain a main class
What you should be doing is providing those two library paths as arguments to the -cp option of the javac command.
This is where it gets quite tricky, as you are mixing a Linux-style shell emulator with a Windows JVM. Paths that are intended for the shell must remain in Linux style, while paths that are going to be consumed by the JVM must be converted to Windows format, and path strings for the JVM must be delimited with semicolon (Windows style) instead of colon (Linux style). That introduces a further complication since the semicolon in Cygwin (Linux) is the delimiter for multiple commands on one line, so the path string must be quoted to prevent the semicolon from breaking things.
Also problematic is the naming of the class to be compiled. You have not shown us the package declaration of the Java file, but I'm assuming it's in the default package (i.e. there is no package declaration and it's not package src;). In that case you should be in the src directory, not one directory above.
Finally, once you specify -cp, you must also add the current directory to the classpath on Windows if you want it to be included, otherwise it will not find your newly-compiled .class file.
So the compile and execute commands should be
javac -cp '../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester.java
java -cp '.;../lib/javax.json-1.0.jar;../lib/javax.json-api-1.0.jar' TimeTester
For simple relative paths the Windows JVM will accept forward slashes, but if you have absolute Linux paths (i.e. /cygdrive/c/..., or with the cygdrive path set to /, paths like /c/user/...) the JVM will not understand them and they will need to be translated using cygpath.
None of your 4 commands work:
java -jar ./lib/javax.json-1.0.jar does not work because javax.json-1.0.jar is not an executable jar file.
java -jar ./lib/javax.json-api-1.0.jar does not work because javax.json-api-1.0.jar is not an executable jar file.
javac ./src/TimeTester.java does not work because your class requires classes from the javax.json package to be on the classpath, and you haven't set the classpath. Classes from the javax.json package are found in the javax.json-1.0.jar file.
java TimeTester does not work because the compilation failed.
To fix all that, remove the first two lines, and specify the classpath on the other two lines, e.g.
javac -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar ./src/TimeTester.java
java -cp ./lib/javax.json-1.0.jar:./lib/javax.json-api-1.0.jar:./src TimeTester
Notice that you also had to list ./src on the classpath when executing your program.
I'm trying to run a script from an Amazon Linux machine. The script invokes checkstyle like this (in a script called eval.sh):
CHECKSTYLE="java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar"
CHECKSTYLE_RULES="/home/ec2-user/grader/config/checks.xml"
CHECKSTYLE_OUT="quality.log"
"${CHECKSTYLE}" -c "${CHECKSTYLE_RULES}" -f xml -o "${CHECKSTYLE_OUT}" $(find "${_toCheck}" -name "*.java") 2>"quality.err"
When I run this, I get the following error in quality.err:
./grader/eval.sh: line 10: java -jar /home/ec2-user/grader/ext/checkstyle-6.15-all.jar: No such file or directory
I have tried to run the same command directly in the terminal and it is working. Both checkstyle-6.15-all.jar and checks.xml are where they should be.
What could cause this problem?
Change "${CHECKSTYLE}" to ${CHECKSTYLE} (without the quotes).
You are passing the entire value of the CHECKSTYLE variable as a single word (that's what the quotes do), so the shell is looking for a relative directory named java -jar, and is trying to find a file under that (nonexistent) directory with the path home/ec2-user/grader/ext/checkstyle-6.15-all.jar.
When you envoke "${CHECKSTYLE}" the shell thinks that is the command you are running. There is no such file name with the spaces and options have you have included there. If you envoke it simply as ${CHECKSTYLE} (drop the quotes) the shell will process it for whitespace as normal and split it into the appropriate pieces for creating the process.
I am running a Java program with the following command:
java -cp .:./* com.bot.fix.botclient
All the jar files are in the same directory. It works FINE! But what if I want to run it from a different folder?
The full location of the java program is: FIX/fixprog/src/com/fix/botclient
But if I try to run:
java -cp FIX/fixprog/src/* FIX/fixprog/src/com.bot.fix.botclient
I get:
Error: Could not find or load main class FIX.fixprog.src.com.bot.fix.botclient
What am I doing wrong? How can I run the same Java program but not in the same directory?
If you have only jar files try:
java -cp FIX/fixprog/src/* com.bot.fix.botclient
If you have also classes you should try:
java -cp FIX/fixprog/src/*:FIX/fixprog/src/ com.bot.fix.botclient
If both did not work perhaps you shoud use absolut path with disk unit if you are using windows.
Try if below works. You don't need to specify path when giving the fully qualified name of your java class that you are trying to execute. The "cp" part takes care of it.
java -cp FIX/fixprog/src com.bot.fix.botclient
I am following this tutorial to make a program that interacts with the database. I am stuck at the last step where I run it. The given example is
C:\test>java -cp c:\test\postgresql-8.3-603.jdbc4.jar;c:\test JDBCExample
I have both the .class file and the .jar for the JDBC in my home directory. I tried
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF/QueryDB.class and I get "cannot exectue binary file"
I tried
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF/QueryDB and I get "no such file or directory"
I tried
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF QueryDB and I get "JohnF is a directory"
I used chmod to set the file permissions to 777. How do I get this to run?
You are using semicolon as classpath separator - this will not work on Linux.
Try replacing ";" with ":" in classpath and it should work.
Edit: explanation of what is happening here.
In Linux, ";" is command separator. Your line of
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar;/home/JohnF QueryDB
is really expanded into 2 executed one by one:
java -cp /home/JohnF/postgresql-9.2-1000.jdbc4.jar
/home/JohnF QueryDB
First one does nothing and successfully quits.
Second tries to invoke /home/JohnF as executable, and this is really not an executable, but a directory!
FIX: Use : instead of ;
WHY? The file-separator in *nix environment is ':' and not ';'
this is the command.
java -cp clojure.jar;sum.jar CalculateSum
sum.jar is a jar file made from clojure and java code.
CalculateSum is file which contains main method of java.
error from cygwin
can't execute binary file, Error 126
Cygwin provides you with a *nix environment within Windows, so that you might have to change the classpath separator to colons:
java -cp clojure.jar:sum.jar CalculateSum
Try:
java -cp clojure.jar:sum.jar:. CalculateSum
If you execute in the place you have the root of packages for CalculateSum.class
java -cp "clojure.jar;sum.jar" CalculateSum this is working