This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android 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: 14074
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run a java class in console
 
Similar Threads
Regex for capital letters
Pattern & Matcher
Metacharacter looks for alphabetics
Code: regex, formatted output, console
Doubt about Regex and Quantifiers