Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to run a java class in console

 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 251
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic