jQuery in Action, 3rd edition
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
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

How to run a java class in console

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 245
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
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: 238

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: 15092

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 8 API documentation
I agree. Here's the link: http://aspose.com/file-tools
subject: How to run a java class in console
It's not a secret anymore!