How to run Java program in command prompt - java

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

Related

I'm trying to run java from cmd with help of VSCode and it doesn't find the other classes I created [duplicate]

At the moment I am looking for another way to run my Java program from command line, other than adding it to a JAR file. My program has the following number of classes:
The name of the program file - MyProgram
Main class - Server1
second class - Client Handler
Package name - Items
3rd class - User1
4th class - User2
The main class and client handler alongside the package will have to run first in order for user 1 & user 2 to run, because they are client classes and are dependent on the main class.
javac *.java // compliles all java files in the dir
java MyClass // runs the particular file
If one class is dependent on another class that hasn't been compiled yet, the program won't run. So you should compile all files before trying to run the program dependent on other files.
If your files are packaged, then something like this
javac com.mypackage/.*java
java com.mypackage.MyClass
you must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath. Note that the windows classpath separator is a semi-colon ie ;
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
Example. Suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
Java file is located inside "src/com/test/Hello.java"
then, from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
Note that you can add -d to specify output directory of your class files whenever compiling
$ javac -d output_directory -cp . com/test/Hello
In windows the same thing will be working too, I already tried
Check out this from Oracle official site
Once you compile your code, you then run this from the top level:
java -cp . com.myprogram.MyProgram
That order thing you describe doesn't matter. They all get compiled together, and MyProgram will reference Server1, etc.
It may be more then you want to tackle right now but you might want to consider a build system like Maven. To start try out; How do I make my first Maven project?
You can use it to predefine the build order and if you want have it create a jar for you (or not).
Sounds like you will just need to open multiple command prompts and compile and run them in the order you need them to run. Let me know if I misunderstood question.
TO EXECUTE TWO JAVA PROGRAMS WHICH DEPENDS TO EACH OTHER.
(for example:two files Complex.java and Solution.java, where Soultion.java depends upon Complex.java.
So Complex.java should be compiled first and then the class file of Complex must be linked with Solution.java and then Solution.class must be executed for Output.)
REFER THE IMAGE WITH SYNTAX.
STEP 1:
COMPILE Complex.java
compiling Complex.java
syntax-
javac -d [path_where_class_File_build] [path_of_the_file\filename.java]
(Solution.java and Complex.java are Linked. ie-Solution.java calls Complex.java)
STEP 2:
COMPILE Solution.java
compiling Solution.java with linking Complex.class
with linking Complex.class(above created in step 1)
syntax-
javac -d [path_where_class_File_build] -cp [path_of_the_first_class_created] [path_of_the_file\filename.java]]
STEP 3:
EXECUTE THE Solution.class
java -cp [path_of_second_class_created] [class_Name]
(created in Step 3)

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.

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.

Run java program sing Cmd

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.

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

Categories

Resources