This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes How to run a java class in console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to run a java class in console" Watch "How to run a java class in console" New topic
Author

How to run a java class in console

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 240
I have a class in directory c:\Workspace\RegExTest\bin\RegexTestHarness.class

if I go to directory c:\Workspace\RegExTest\bin and then type
java RegexTestharness
the class can be run successfully.

but if I run in another place and try to run like
java c:\Workspace\RegExTest\bin\RegexTestHarness
or
java -cp c:\Workspace\RegExTest\bin\RegexTestHarness

both return error, so what is the correct syntax to run the class without go to its directory first?
Also, if run in eclipse, there is error "No console."
the class is just copy from tutorial as follow:
Thakur Sachin Singh
Ranch Hand

Joined: Jun 15, 2010
Posts: 232

set classpath variable for javac and java command .

for this set PATH=C:\Program Files\Java\jdk1.6.0\bin;

now you can compile and run the file on windows machine.


SCJP 6- 91%, IBM DB2, IBM RAD Certified
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14113
    
  16

Try a command like:

java -cp c:\Workspace\RegExTest\bin RegexTestHarness

Note that the parameter to the -cp is the directory that contains the class file (c:\Workspace\RegExTest\bin). After that is a space, and then the class name.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to run a java class in console