How to run java class file which is in different directory? - java

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

Related

Cannot run Java app on cmd using Java11 and Windows

I ahve tried several approaches as on How do I run a Java program from the command line on Windows? and create exactly the same class and packages to use the same things.
Here is the sample class located on C:\SimpleJavaProject\src\com\hello\programs:
package com.hello.programs;
public class ABC {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compile it in the usual way:
C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java
Later, run it by giving the package name and then my java class name:
C:\SimpleJavaProject\src > java com.hello.programs.ABC
In each time I try to run java app, I get "Error: Could not find or load main class com.hello.programs.ABC" error. Then have a look at What does “Could not find or load main class” mean? page and tried some approaches on that page. But still the same error.
It is too simple, but still I have not managed to run the simple app yet. So, how to fix this problem? And after running the app, how can I pass args on cmd?
Update: I could already generate ABC.class file by running the following command. BUT, I cannot run the app and see the "Hello world" on the console.
cd C:\SimpleJavaProject\src\com\hello\programs
javac ABC.java
--> generates ABC.class in C:\SimpleJavaProject\src\com\hello\programs
java com.hello.programs.ABC
When you run this, java is going to check each and every CLASSPATH path for that + /com/hello/programs/ABC.class, will load that, and then run it.
This must mean that either:
[A] Your classpath does not include the current directory; The fix is java -cp . com.hello.programs.ABC.
[B] you didn't do what you wrote, and e.g. dir com/hello/programs.ABC.class prints nothing.
Note that you're not doing it right; a class file should never be in a directory path that includes src. If you don't want to bother with build tools like maven or gradle, I strongly suggest you don't bother with a src dir then either. If you must, the -d option can be passed to javac to tell it where tou put the file. If you want to separate source and class files, then that should be targeting a directory named bin or build or whatnot (a sibling of the src dir).
When you use javac ABC.java you are compiling the class, and javac places it in the current directory.
So java com.hello.programs.ABC would not work (because com/hello/programs/ABC.class file does not exists).
You can use the javac -d flag:
-d <directory> Specify where to place generated class files
For instance:
> javac -d . ABC.java
> java com.hello.programs.ABC
Hello world
> cd com\hello\programs
> dir
ABC.class
Would work, because javac did place ABC in com/hello/programs.
Update: For clarity, once you compiled using javac -d . ABC.java you can run it using java com.hello.programs.ABC and you should see Hello world in the screen.

how to run a java class in another folder with jar file

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.

java -cp . test.java Vs java test.java

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

adding JAR class path in UBUNTU

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

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