aspose file tools*
The moose likes Beginning Java and the fly likes System.console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.console" Watch "System.console" New topic
Author

System.console

scott wakeling
Greenhorn

Joined: Aug 27, 2007
Posts: 3
Hi all, this is my first post! Seems like a friendly place so I will be asking all my newb Java questions here as I get ready for SCJP. =)

During my experiments, I have tried to compile a simple program for exploring regular expressions, as provided in the Sun tutorials at: http://java.sun.com/docs/books/tutorial/essential/regex/test_harness.html

I just get "No console." when I run that (NB5.5 JDK 1.6). If I try to run it from the command line prompt, it raises a NoClassDefFoundError for "regextester", which is the name of the project/jar, but not the name of my main class, which is "Main".

Why would System.console() return null? And why is it trying to load a class named "regextester" at the command line? My jar should only have one class inside it named "Main", correct?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Welcome to JavaRanch!

Are you running it from inside NetBeans in the first case? NetBeans probably runs it in such a way that it is not running with a command prompt window. I'm not a NetBeans user so I don't know how you could change this.

How did you build your JAR file, and how are you exactly running it (what is the command you type in to run it)?

Note that you don't necessarily need to create a JAR file to run a Java application. You can try copy-and-pasting the class RegexTestHarness on the tutorial page and saving it in a file RegexTestHarness.java. Then compile and run it on the command line like this:

javac RegexTestHarness.java
java RegexTestHarness


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
scott wakeling
Greenhorn

Joined: Aug 27, 2007
Posts: 3
Hi Jesper, thankyou for the swift reply. I was running it from inside NB in the first case. Does anybody here know how one can make apps that utilise System.Console run from inside NB?

I built the JAR file by compiling the project inside NB. Then at the command line, I used java -jar regextester.jar This raised NoClassDefFoundError for the class "Main", which is odd. I know this exception means the class was there at compile time, but can no longer be found.

I *thought* my classes were all bundled into the jar file, and you just ran that file. However, I have to go to build\classes and use java regextester.Main, to run build\classes\regextester\Main.class - which works as expected.

So it seems I now stray into "how do I redistribute my Java apps" FAQ territory. =)

Thanks for your help.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 376
I'm close enough to a newbie to identify - I still have some NoClassDefFoundError nightmares. Yes, you do have to take account of packaging when running a java app - you got that right - take special note - java won't find your app and you'll get NoClassDefFoundError if you don't tell it where to look or give it the wrong name. Also happens quite frequently with relatively simple applications if you don't run with -cp . ('.' isn't a period at the end of my sentence; it's setting classpath to current directory.

Note that I found no class named Main in the tutorial, so you should not be trying to compile or run Main. You should have no file named Main.anything. There is a main method in class RegexTestHarness. The main method executes automatically when RegexTestHarness is run. The main method is required for stand-alone applications.

As Jesper mentions above, the tutorial example does not require a jar file or any additional complexities. It's not defined as being in a package, so if you just copy RegexTestHarness to a file and save it as RegexTestHarness.java, you can compile and run very simply; just as he explained.

Hope that helps a little.


Correlation does not prove causality.
Vamshi
Greenhorn

Joined: Oct 27, 2008
Posts: 1
Hi I am having the same problem .

I am running it on my eclipse which has been configured to JRE 1.6

When I run the program it gives me " NO Console ", but when I run it from Command Line it works absolutely fine.

Can any one enlighten me about why and what is happening ?

Thanks
Vamshi
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

if you are indeed new to java, i would suggest ditching Eclipse, NetBeans or any other IDE for doing much more than simple editing. learn how to compile and run your code from a command line FIRST. Once you understand the fundamentals, you can move over to the IDE world.

One of the reasons is that right now, you are struggling to learn BOTH the IDE and Java, so it's hard to separate out where the real issue is. A second reason is that the IDE can hide a lot of the issues from you, so you don't get the solid grounding you need.

just one developers 2-cents.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Vamshi:
Hi I am having the same problem .

I am running it on my eclipse which has been configured to JRE 1.6

When I run the program it gives me " NO Console ", but when I run it from Command Line it works absolutely fine.

Can any one enlighten me about why and what is happening ?

Thanks
Vamshi


I agree with Fred, start out using the command prompt, move to IDE when you understand Java basics.

This appears to be an IDE thing. The Console was added in Java 1.6. Not sure what version Eclipse was created in, but it appears to not yet release its input/output as a Console object.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
For some reason, "Vamshi". the private messages aren't working, so I shall have to tell you it is about the naming policy: we require you display first name-space-last name; please go to "my profile" "update profile" and correct your displayed name to comply.

CR

And Roger Gay, please don't hijack somebody's thread by posting an unrelated question. YOu ought to have started a new thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
And Roger, I seem to have misunderstood your reply. I thought you were hijacking; now on reading the thread again I think I was mistaken.

I am very sorry about that mistake.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by Campbell Ritchie:
And Roger, I seem to have misunderstood your reply. I thought you were hijacking; now on reading the thread again I think I was mistaken.

I am very sorry about that mistake.


Not hijacked, but a zombie. Roger's reply was over a year ago. Vamshi should probably have started a new thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
That explains why he hasn't been annoyed with me, but it was still a bad mistake of mine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.console