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.
Related
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
i have one basic questions around java program execution on command prompt.
On some machines we need to specify -cp . (classpath) while executing the java program
(test is the java file name and .class file exists in the same directory)
java -cp . test
whereas some machines do not require it
java test
although both the machines have path variable defined in environment variables, have same jdk version
If you want to run the code with your classpath then we are using the java -cp.
Also if you have an runnable jar and you want to run the class which is not use as runnable class at the time of jar creation then also you use the java -cp
If you directly want to run your java file without class path and also the runnable jar as well you can directly use java command.
cp command is classpath command-line .If you don’t specify classpath,the default classpath is the current directory. If you want to include the current directory in the search path, then you must include a dot (.) in the new settings.
Class path entries that are neither directories nor archives (.zip or JAR files) nor the asterisk (*) wildcard character are ignored.
For more, you can see Setting the Class Path
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.
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