• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

System.console

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
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
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
 
scott wakeling
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That explains why he hasn't been annoyed with me, but it was still a bad mistake of mine.
reply
    Bookmark Topic Watch Topic
  • New Topic