Running a java program through command line - java

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

Related

Java don't run the main file (classpath)

Hello guys i'm new with java and i'm starting to work with packages etc, i have a problem when i try to run my main file located at
C:\Users\Robert\Desktop\Java\latebd\Test.Java
When i run
java -cp C:\Users\Robert\Desktop\Java\latebd\latebd.Test
instead of having the code running i get this (it's in italian bu basically it's telling all the options when i use java -
Why do i get that?
you need to compile it e.g. javac C:\Users\Robert\Desktop\Java\latebd\*
provide the path where it's already compiled as a classpath, and to run it use java -cp "C:\Users\Robert\Desktop\Java\latebd\*" latebd.Test
Try java -cp "C:\Users\Robert\Desktop\Java\latebd\latebd.Test"

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.

Compiling programs with packages in java using windows command line

I have a .java file which i compiled in a package named "Mypack",using command line as follows
javac -d . file_name.java // The "." specifies the current working directory which was the desktop
so it creates a folder on the desktop named "Mypack"(The package name), in the folder the .class file for my program is placed.Now i did the following
java -classpath "C:\Users\LoRd CuRZon\Desktop\Mypack" file_name // Error Could not find or load main method
Even if i go into the directory "Mypack" and launch command prompt from that directory and try to run the program i still get the same error.
run it as likewise from Desktop,
c:/.../Desktop> java Mypack.file_name
java command requires fully qualified name .
so from desktop run java Mypack.classname
If you have this error:
Error Could not find or load main method
That means you don't write a main method in you code try to write it.
But befor to do those steps:
Fo compiling a programme do this:
java Mypack.file_name
To run it do like this:
java Mypack.classname

Run A Java Program That Calls On An External Jar

I'm trying to run a Java program that calls on the C:\Users\Brandon\Downloads\commons-io-2.4 folder. It compiled fine with this code javac -cp "C:\Users\Brandon\Downloads\commons-io-2.4\*" Login.java but when I tried to run it with this code
java -cp C:\Users\Brandon\Downloads\commons-io-2.4\* Login it gave me thiserror: Could not find or load main class Login.class How do I fix this?
P.S. The program calls on the FileUtils.
You need to add the current location as part of classpath.
java -cp .;C:\Users\Brandon\Downloads\commons-io-2.4\* Login
symbol .(dot) is the current path, separated by comma
Try this: java -cp C:\Users\Brandon\Downloads\commons-io-2.4\*;. Login
You have to add the current directory to the class path
If that doesn't work try java -cp C:\Users\Brandon\Downloads\commons-io-2.4\* -cp . Login

Error: Could not find or load main class xxx Linux

I am very new to linux environment.
I am trying to run an simple hello world java class in linux environment.
Hello .java
package com.util;
public class Hello {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("hi");
}
}
I have compiled java class in windows environment and uploaded the .class file to linux system into /home/scripts path.
my command is as follows,
java -cp /home/scripts com.util.Hello
when i am executing this command from this same /home/scripts where Hello.class is there i am getting,
Error: Could not find or load main class com.util.Hello and not able to proceed further.
can some one help me in this issue?
navigate to /home/scripts using terminal
javac com/util/Hello.java
then
cd /home/scripts
java -cp . com.util.Hello
Or,
java -cp "/home/scripts" com.util.Hello
At first you must generate your .class file :
javac ./hello.java
This command has generated hello.class file
And after you can run your class file ! :)
java hello
We first know javac command work well.
I also met this error,and i have resolved this.Let me share this.
First we need to find the parent path of your package in your java codes.
Then cd to that path using java package + fileName should work well at that moment.
I had the exact same issue on windows, and I solved it by adding path "." to both CLASSPATH and PATH, maybe you can try this on Linux as well.
Your .class file should not reside in /home/scripts/, but in /home/scripts/com/util/. Take a look at this document that explains the relation between classpath, packages and directories.
Before Specifying the path,ensure you follow these three things meticulously,
1. Close the command prompt window, before specifying the path.
2. When adding path, add bin and semi- colon at the end and
3. If JAVAC command has worked properly, try java -cp class name.
if you want to run program in current working directory where your class reside.
java gives three options.
first option
java -cp Tester
Second option for current working directory
java -cp . Tester
Third option export CLASSPATH variable
export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or
export CLASSPATH=$PWD
or
export CLASSPATH=
after that you must sorce the bashrc or bashprofile.

Categories