I'm trying to run Java program using cmd.
When I compile that file using
javac helloworld.java
It works perfectly but when I try to run that file using
java helloworld
I get an error:
couldn't find or load main class.
even though my I put my javac path in class path in system variables and javac working correctly.
After searching on how I can fix it I found that I can use
java -cp . helloworld
because it let you to find .class file.
and it works but I know that I can run java program without this -cp so what this for and how I can run my program without it?
-cp specifies the Java classpath for the JRE you are attempting to start. Look for an environment variable CLASSPATH and add '.'.
-cp is used to set the classpath for the jar file, this flag is same as importing a jar file to eclipse and then use it.
If you want to run without this flag, use should set the classpath first beforing running.
export CLASSPATH=path/to/your/jarfile.jar
If you already have some classpath set
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar
If you want to include current directory
export CLASSPATH=$CLASSPATH:path/to/your/jarfile.jar:.
As other have mentioned, you can set the CLASSPATH. However, a much better approach is to bundle the .class files in a JAR ans user java -jar nameofthejar.jar.
Related
I downloaded example.jar and I can type java -jar example.jar from within the directory where it is located and it works.
The problem is that I need to be able to call it from elsewhere without typing the full path. Is it possible?
I tried adding it to $CLASSPATH like this:
export CLASSPATH=$CLASSPATH:/Path/to/Directory:/Path/to/Directory/example.jar with no success.
Yes. Option 1. Using the CLASSPATH you have set, however you would have to specify the fully qualified main-class from the jar
java com.mypackage.MyMain
As long as com.mypackage.MyMain is on the CLASSPATH and contains a valid main method, that will run it.
Option 2. Create a bash shell script to run it (note that this is really providing the full path to the java command)
#!/usr/bin/env bash
export JARFILE="/Path/to/Directory/example.jar"
java -jar $JARFILE
I have the classic :
java.lang.NoClassDefFoundError: org/python/util/jython
Even if the jar jython.jar exist in the environment variable PATH for my user and system wide path.
How can I quickly test that I am not crazy and it is indeed in the path?
Java doesn't use the PATH variable.
To specify the classpath when running a java application use the -cp parameter for the java command.
Inside your batch file (the .cmd file) find the java command and add the needed jar file:
java -cp somefile.jar;\path\to\jython.jar someclass.MainMethod
Please don't use the deprecated CLASSPATH any more.
For more details, please see the Java documentation:
http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html#tooloption
use the following command
set classpath="path to your jar/jython.jar";
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
I am trying to do "javac Classname.java" from cmd prompt, and this Classname.java requires Jfreechart libraries/jars, and runs fine if compiled from Eclipse (because project package has jars imported).
But I want to run the file from cmd prompt and its not able to show me the output. It comes with errors like: ("package doesn't exist"), how to fix it? I need the class file and also run JNI commands to create header file.
You need to set the classpath.
You can do this in 2 ways. Either use the -classpath or -cp option:
javac -cp jar1.jar;path/to/jar2.jar Classname.java
Or, if you need it to persist, use the CLASSPATH environmental variable:
set CLASSPATH=path1;path2
javac Classname.java
If you have already managed to run your code in Eclipse, then Eclipse can help you.
In the "Debug" view, you should have something like this remaining after you have run your code:
If you right-click the bottom "terminated" text and select "Properties", you will get something like this:
You can copy the command line content and use that to run your app from the command line, or use it to set the classpath as the other answers have advised.
You just need to add the directory paths and/or .jar libraries to your "-classpath" command-line argument.
Depending on how many libraries you've got, you might well wind up with a .sh script (Linux) or .cmd file (windows) that looks something like this:
http://ubuntuforums.org/showthread.php?t=230258
java -cp jts.jar:jcommon-1.0.0.jar:jfreechart-1.0.0.jar:jhall.jar:other.jar:rss.jar -Xmx256M jclient.LoginFrame .
If you're on Windows, you'd use ";" as a separator (instead of *nix ":").
'Hope that helps!