Running Java program from Linux command line that requires an additional file - java

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 ';'

Related

Cygwin terminal error in running .sh file because of a jar file

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.

Cannot compile java file using javac command under JDK on ubuntu (APARAPI)

I have run the same command under windows using the windows Java Development Kit and it worked.Now i try to run it ubuntu and i get this error.Please help
Different OS have different dist package manager, you try to install Java environment in your Linux system by below Reference
It looks like you are specifying the -cp argument incorrectly.
On Linux, the path separator character is ":" not ";". The ";" character separates commands on the command line, assuming that you are using bash or similar as your shell.
It looks like your command has been interpreted as two commands:
$ javac -g -cp ../dist_windows_x86_64/something
$ . GPUGalaxySim.java
The javac command fails because there are no source filenames on that command line ... just like the error message says.
Then the second command:
The dot command (".") is a built-in shell command that "sources" a file and attempts to interpret it as shell commands.
If you attempt to "source" a file that is actually Java source code, you get nonsense error messages, basically because the shell has no clue what Java code means.

cannot execute binary file: Exec format error

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 ";".

Connecting to MySQL database using Java in command prompt

Since I don't have netbeans right now, I am trying to connect to MySQL database from my Java code through command prompt. But it it is not taking the mysql-coonectivity.jar file.
Does anyone know any way to run my program??? Please help.
Try executing the program as it is windows OS
java -cp .;path\of\your\mysql-connector-java-5.1.6.jar className
In case you don't have the mysql-connector-java-[version].jar get it from here
You need to add your MySQL connector jar file while compiling and running your program, you can do it the following way,
To compile :
javac -g -cp mysql-coonectivity.jar; YourFileName.java
To Run
java -cp mysql-coonectivity.jar; YourMainClass
NOTE: The above written syntax assumes that your jar file is present at same location as of your program.
To execute program in Linux (Ubuntu) OS follow the below command format:
java -cp .:/usr/share/java/mysql-connector-java-8.0.25.jar MainProgram
Note:
Assuming you are in the directory where MainProgram.class is exist
After .(dot -- this is for current directory) there is :(colon) as a separator in Linux

How do I compile Java code on Centos 5?

In windows I can use this command:
java -cp server.jar;mysql.jar server.NithServer
But in linux it says:
-bash: server.jar: command not found
How can I add both server.jar and mysql.jar in Centos 5?
You need to use : as the path separator, instead of ;
In bash the ';' character separates shell commands when you want to put them in the same line.
This means that the string that appears after ';' and before the next space is considered a new command.
This is why, in the Unix java command, ':' is used as a separator for jars in the '-cp' option. This also happens in other Unix commands as well.
So you should write:
java -cp mysql.jar:server.jar server.NithServer
On another note, if you have these kind of difficulties again, try to call the 'man' command followed by the command you want to understand.
man java
There, you will find the answer to your question, right where the '-cp' option is described.
Enjoy.

Categories