I'm trying to invoke a java class that is not in current directory. Ive set -classpath flag and ensured that the class file is also there in the path. But im getting the following error
Error: Could not find or load main class Fileversion
Bat file code
java -classpath x:/LCMSLatestLibrary/Fileversion/Fileversion.class Fileversion H:\LCMS_Jars\client.jar x:\LCMSLatestLibrary\64bit\client.jar
pause
The same works if I cd into x:/LCMSLatestLibrary/Fileversion and execute the above line without -classpath flag. What am I missing?
You have to set as classpath the jar or directory containing your packages.
In your case (assuming the class Fileversion is in the default package) the command should be:
java -classpath x:/LCMSLatestLibrary/Fileversion Fileversion H:\LCMS_Jars\client.jar x:\LCMSLatestLibrary\64bit\client.jar
Related
i'd like to run a java class on other folder, i have a mysqlcon.jar on the current path and a PetsGUI.class on ./classes/
when i try to run it by doing
java -cp .:mysqlcon.jar -d classes/PetsGUI
i receive
classes/PetsGUI not found
if i move mysqlcon.jar on classes and type
java -cp .:mysqlcon.jar PetsGUI
on classes/ it runs, so the code is correct. what's the correct command to run it?
So you need the jar file, and the classes directory in the classpath:
java -cp ./classes:mysqlcon.jar PetsGUI
java doesn't expect a file path as argument. It expects the fully quelified name of a class. And that class is then searched on the classpath.
I have checked my java installations by compiling and running a HelloWorld program which works perfectly fine.
The problem comes when I compile my program with certain jar files which are located in the same directory as my java file. This is what I've done.
javac -cp "A.jar:B.jar" MyProg.java
This generates the class file MyProg.class successfully. Next when I run the following command, it gives this error error: could not load or find main class MyProg
The command is:
java -cp "A.jar:B.jar" MyProg
Next, I even tried next by moving the jars in a folder named lib and issued the following commands:
javac -cp "lib/*" MyProg.jar (works fine;generates a class file)
java -cp "lib/*" MyProg (issues the same error)
I am working on a linux machine. Can some one please resolve the error.
Add the current path to the classpath
java -cp .:A.jar:B.jar MyProg
Directory path:
c:\home\test\src\com\bsoft\conc
I have my java program in src folder and I have my class file in conc folder.
I need to run my java program from home folder.When I run I'm getting error:
could not find or load main class
Set your class path for this java file:
java -cp C:\hello\build\classes com.javahowto.test.HelloWorld
or using Environment variables and run it from any third location from that machine.
It's time for you to read on about classpath ( a way to tell java compiler where to look for the class file you intend to run ).
Basically there are two ways to set classpath
a environment variable CLASSPATH having ':' separate directories in unix and ';' separated directories in windows
-classpath or -cp command line arg to javac command
Refer and read the below links completely
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
In my program com.bsoft.conc is a package name where my class file for the compiled program will be stored.If I have to run that from home folder we have to specify
java -classpath test\src com.bsoft.conc."class-file-name"
This is because we need to tell the JVM where it has to look for class file.
so , we have to specify navigation to the src using "test\src" and then
class file location "com.bsoft.conc.class-file-name"
If you have set environment variable in advanced settings then it will also be overriden if you specify classpath in cmd
I had a similar problem where I was trying to run a Java program that calls a method in a class that is in another directory. I read this page and added the directory to my classpath, but I made the mistake of using '~' which in Bash means '/home/user/'.
So this command did NOT work
java -classpath ~/CurrentDirectory:OtherDirectory program
But this command did
java -classpath /home/user/CurrentDirectory:OtherDirectory program
The answers here don't touch on the spaces problem.
If your path has spaces, you must wrap it inside the quotes, otherwise an error would show up:
C:
Program Files
MyProject
src
testpackage
Test.java
target
classes
testpackage
Test.class
package testpackage;
public class Test {
public static void main(String[] args) {
System.out.println("Test");
}
}
java -cp "C:\Program Files\MyProject\target\classes" testpackage.Test
In Linux ( Simple scenario with out consider them as packages )
> parent
|
|- stdlib
| |
| -Library.java
|- Main.java
COMPILE FROM PARENT > javac -cp ./stdlib: Main.java
RUN FROM PARENT > java -cp ./stdlib: Main
This is might be a common question but I am not able to add class path for a JAR file in UBUNTU. I have given below all the details I know:
java is located here:
the o/p of which java command is - /usr/bin/java
sudo vim /etc/bash.bashrc
export CLASSPATH=$CLASSPATH:/downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar
ps: downloads folder is directly under the root
sudo vim /etc/environment
CLASSPATH="/usr/lib/jvm/jdk1.7.0/lib: /downloads/aws-java-sdk-1.3.24/lib/aws-java-sdk-1.3.24.jar:"
As you can see, I have added the class path in bashrc and etc/environment... but still I am getting an error while trying to run the S3Sample.java which comes with awssdk for java.
when I compile the java file, I get the following errors:
ubuntu#domU-12-31-39-03-31-91:/downloads/aws-java-sdk-1.3.24/samples/AmazonS3$ javac S3Sample.java
S3Sample.java:25: error: package com.amazonaws does not exist
import com.amazonaws.AmazonClientException;
Now, I clearly understand that the JAR file is not added to the class path and so I am not getting the error. I've also tried javac with the class path option - but it does not work :(
PS: JAVA home is set correctly as other java programs work properly.
To set the classpath, it is in most cases better to use the the -cp or -classpath argument when calling javac and java. It gives you more flexibility to use different classpaths for different java applications.
With the -cp and -classpath arguments your classpath can contain multiple jars and multiple locations separated with a : (colon)
javac -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass.java
java -cp ".:/somewhere/A.jar:/elsewhere/B.jar" MyClass
The classpath entry in the example sets the classpath to contain the current working directory (.), and the two jar files A.jar and B.jar.
If you want to use the CLASSPATH environment variable you can do
export CLASSPATH=".:/somewhere/A.jar:/elsewhere/B.jar"
javac MyClass.java
java MyClass
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