Classnotfound when running program from command prompt - java

When I run my java program from the command prompt, the program does not not run because of a ClassNotFound error. But the class I see is there in a jar file which is on the classpath. That same program when I run from Eclipse works.Not sure as to what I am missing here.Thanks.I was trying to run the following command from my windows dos prompt:C:\softwares\SpringBatchExample4>java -cp "target/dependency-jars/*:target/spring-batch.jar" org.springframework.batch.core.launch.support.CommandLineJobRunner spring/batch/jobs/job-read-files.xml readJob.And I looked into one of the jars under target\dependency-jars to confirm that the jar file has the CommandLineJobRunner class.

When running a JAR file from command line you have two options for specifying the classpath:
Using the -classpath or -cp command line argument, followed by a space and a list of full paths to all classpath entries delimited by a semicolon (Windows) or colon (Linux/Unix);
Using the Class-Path entry in the main JAR manifest where other rules apply.

If there is no Main-Class entry in your manifest file, you might need to specify the class containing the main method on the command line like this:
java -cp MyJar.jar com.example.MyMainClass
See also:
How to run a class from Jar which is not the Main-Class in its Manifest file
execute a main class from a jar

Related

Cannot run a .jar file

I'm having issues when I try to execute a .jar file. I always get the message from the command prompt "cannot find or load main class" every time I try to run the file I have just compiled.
I have tried the following(using the command prompt and saving the java code with Notepad++ and creating the manifest.txt file in the same folder where the .java code was saved):
javac classname.java
java cfm classname.jar manifest.txt classname.class
java -jar classname
I have also tried:
javac classname.java
java classname
And:
java packagename.classname
After compiling the .java file
I know there are hundreds of questions like this in the forums, but I have looked everywhere and I keep getting the same error. I just cannot run the .jar file even after I create it.
It's probably something I have missed but I am out of ideas.
Manifest:
Main-Class: classname
Let's see.
java -jar classname.jar
would work if your MANIFEST.MF calls out the correct class.
java -cp classname.jar my.package.MyClass
would work in any case.

How to include JAR file in a java program that is being executed by command line (Windows)

I know that there are similar posts to this, but I tried all suggested answers there and they didn't work out (maybe I did something wrong). I have a java project with the following folders:
bin
src
in the bin folder, I have few folders with .class files in them (compiled the project with eclipse, now just need to run it with command line). These are the folders inside bin:
client
game
protocol
threadPerClient
tokenizer
I also added the jar file (gson-2.2.1) to the bin folder. My main class is at threadPerClient folder, and this class name is MultipleClientProtocolServer. The main class get an int as a parameter. These are the commands that I tried to run via command line, from bin folder:
java -cp gson-2.2.1.jar threadPerClient.MultipleClientProtocolServer 4000.
And I got for that:
Error: Could not find or load main class threadPerClient.MultipleClientProtocolServer
java -cp threadPerClient/:./ MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class MultipleClientProtocolServer
java -cp threadPerClient;. com.example.MultipleClientProtocolServer 4000
And I got for that:
Error: Could not find or load main class com.example.MultipleClientProtocolServer
What am I doing wrong?
Make sure you're running command 1. of your question from within the bin folder. Also make sure in -cp you specify the gson jar as well as the current folder:
Unix:
java -cp gson-2.2.1.jar:./ threadPerClient.MultipleClientProtocolServer 4000
Windows should be:
java -cp gson-2.2.1.jar;. threadPerClient.MultipleClientProtocolServer 4000
but I cannot test the command for windows since I don't have a windows machine.

Run a jar using a shell file and specify classpath and properties file path

I've compiled a Spring Maven project JAR. My dependencies are all in a directory labeled lib. How to execute my JAR while specifying it should use the LIB/* as the classpath and also use log4j.xml and a properties file called marker.properties. My marker.sh
LIB=$INSTALL_PATH/lib
echo "$LIB" # outputs usr/local/app/lib
export CLASSPATH=$(JARS=("$LIB"/*.jar); IFS=:; echo "${JARS[*]}")
PARM="-Dlog4j.configuration=/usr/local/Marker/log4j.xml"
java $PARM -jar /usr/local/Marker/marker-1.0.jar
But on running this I get the following error as it is not able to find the jars in the classpath
Exception in thread "main"java.lang.NoClassDefFoundError: org/springframework/context/ConfigurableApplicationContext at java.lang.Class.getDeclaredMethods0(Native method)
However, this command is able to load the jar from classpath, but I am unable to add the properties file
java -cp /usr/local/Marker/marker-1.0.jar:"$LIB"/* com.package.Marker
java -cp /usr/local/Marker/marker-1.0.jar:"$LIB"/* com.package.Marker worked perfectly fine. I needed to add .properties file and log4j.xml in folder where jar was placed
You can specify the classpath using the -classpath (or -cp) flag; for example:
~$ java -jar package.jar -classpath /path/to/classpath/*
For more information, see the following documentation.

running jar that references another jar

Trying to run a jar from the command line that references another jar.
I am able to run the jar without referencing the other jar fine by:
/usr/java6/bin/java -cp /u/me/bin/MyJar.jar com.mypackage.myprogram.ContainsMain
Now if I need to add another jar that MyJar reference so I try this but it fails:
/usr/java6/bin/java -cp /u/me/bin/MyJar.jar;/u/me/bin/OtherJar.jar com.mypackage.myprogram.ContainsMain
I get the error: OtherJar.jar: cannot execute
It seems like as soon as I put a ';' in the -cp argument it fails
I'm on an AIX box if that matters
File permissions are set to 777 on both jar files
On Unix/Linux/related, the path separator is not ;, but :. Revise your command-line to:
/usr/java6/bin/java -cp /u/me/bin/MyJar.jar:/u/me/bin/OtherJar.jar com.mypackage.myprogram.ContainsMain
Since you're on a Unix platform, your classpath separator should be a colon, not a semicolon.
This:
/usr/java6/bin/java -cp /u/me/bin/MyJar.jar;/u/me/bin/OtherJar.jar com.mypackage.myprogram.ContainsMain
is interpreted by the shell as two commands separated by a semicolon, hence your error.

How to run Java program in command prompt

I created a Java project to call a Web service.
It has one Main java file and another class file.
I have used some jar files for HTTP client.
In Eclipse it runs fine.
I need to run the Java program in command prompt by passing some arguments.
In command prompt I went to src folder containing main java and sub class java file and gave the following command
javac mainjava.java
I'm getting following error
mainjava.java:14: cannot find symbol
symbol : class SubClass
here SubClass is my another java class file used to call the web service.
How to run the program by passing arguments?
javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:
java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar
From directory java execute:
java -cp lib/mypackage.jar Main arg1 arg2
A very general command prompt how to for java is
javac mainjava.java
java mainjava
You'll very often see people doing
javac *.java
java mainjava
As for the subclass problem that's probably occurring because a path is missing from your class path, the -c flag I believe is used to set that.
You can use javac *.java command to compile all you java sources. Also you should learn a little about classpath because it seems that you should set appropriate classpath for succesful compilation (because your IDE use some libraries for building WebService clients). Also I can recommend you to check wich command your IDE use to build your project.
All you need to do is:
Build the mainjava class using the class path if any (optional)
javac *.java [ -cp "wb.jar;"]
Create Manifest.txt file with content is:
Main-Class: mainjava
Package the jar file for mainjava class
jar cfm mainjava.jar Manifest.txt *.class
Then you can run this .jar file from cmd with class path (optional) and put arguments for it.
java [-cp "wb.jar;"] mainjava arg0 arg1
HTH.
javac only compiles the code. You need to use java command to run the code. The error is because your classpath doesn't contain the class Subclass iwhen you tried to compile it. you need to add them with the -cp variable in javac command
java -cp classpath-entries mainjava arg1 arg2 should run your code with 2 arguments

Categories

Resources