Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Java program in command prompt

Tags:

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?

like image 314
Vignesh Avatar asked Aug 15 '12 07:08

Vignesh


People also ask

Why I cant run my Java program in CMD?

It means that javac.exe executable file, which exists in bin directory of JDK installation folder is not added to PATH environment variable. You need to add JAVA_HOME/bin folder in your machine's PATH to solve this error. You cannot compile and run Java program until your add Java into your system's PATH variable.


3 Answers

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

like image 112
hmjd Avatar answered Sep 24 '22 17:09

hmjd


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.

like image 33
Shrewd Avatar answered Sep 21 '22 17:09

Shrewd


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.

like image 3
gkuzmin Avatar answered Sep 25 '22 17:09

gkuzmin