Subscribe to Kamikaze Java        RSS Feed
-----

Fun with Java and Linux

Icon Leave Comment
Imagine all of the fun to be had learning how to get/keep my newly reloaded Linux laptop working (zomg good riddance to vista or W7 on laptops) while also installing and learning Eclipse and Netbeans, along with 50000 other free Linux apps! OK I admit the excitement was all caused by me installing 50000 free Linux apps and thinking I had broken the lappie each of the 2-3 times I 'had to' reload. But that's another story I might not tell later.

So I installed the two dominant Java IDE's (Netbeans and Eclipse), grabbed every ebook and tutorial I can get my hands on and ran in to my first errors. If we're not breaking stuff we're not really learning right??

This is code I'm testing is out of the Java Cookbook (I'll update with the edition / author when I get home and get my hands on it again):

import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class regexTestHarness {
	
	public static void main(String[] args) {
		Console console = System.console();
		if (console == null) {
			System.err.println("No console.");
			System.exit(1);
		}
		while (true) {
			
			Pattern pattern =
			Pattern.compile(console.readLine("%nEnter your regex: "));
			
			Matcher matcher =
			pattern.matcher(console.readLine("Enter input string to search: "));
			
			boolean found = false;
			while (matcher.find()) {
				console.format("I found the text \"%s\" starting at index %d and ending at index %d.%n",
						matcher.start(), matcher.end());
				found = true;
			}
			if(!found) {
				console.format("No match found, %n");
			}			
		}		
	}	
}


Eclipse was friendlier than Netbeans, allowing me to enter all of my code with a minor heads up warning that it didn't like the console statements. Attempting to build/debug the code I get this error:
<terminated>regexTestHarness[java application] /usr/lib/jvm/java-6-openjdk/bin/java
No Console

Not sure if this is something I'm doing wrong per Linux or Java I move over to Netbeans. This was not nearly as friendly as Eclipse. Along with the heads up warnings of bad syntax Netbeans added code to each line or tried to force changes to my typing until it was happy with the unrecognized console statements. I'll have to look in to auto-complete or auto-correct settings in Netbeans and see if I can tone that down.

After arm wrestling with Netbeans to get the code entered I apparently was able to get the code compiled without errors.

Here is how the code looks after creating my new Netbeans project:
package regextestharness;

import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        Console console = System.console();
        if (console == null) {
            System.err.println("No console you non linux programming doofnut.");
            System.exit(1);
        }
        while (true) {
            Pattern pattern = Pattern.compile(console.readLine("%nEnter your regex: "));

            Matcher matcher = pattern.matcher(console.readLine("Enter input string to search :"));

            boolean found = false;
            while (matcher.find()) {
                console.format("I found the text \"%s\" starting ", matcher.group());
                found = true;
            }
            if(!found) {
                console.format("no match found, %n");
            }
        }
    }
}



When attempting to run from the Netbeans IDE this is the error I get:
No console you non linux programming doofnut.
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Now I needed to find out why this is getting the same errors in each IDE and see if I'm doing something against the nature of Linux in general. I fired up a terminal and changed in to my src directory. Netbeans isn't configured to compile in to my source directory so the first step now was to run the command:
javac <source file.java>

No error messages and there is a new .class file in the directory listing, so far so good.

And the program runs! No error messages and everything seems to be working fine when I run this from inside a terminal instance.

So the 'console' statements in Java will work in Linux but only inside the terminal program, which I'm assuming represents 'console' in Linux like a command prompt in Windows. I'm still wondering why this won't work from inside the IDE environment since the code is functional and works in general.

If anyone can explain that behavior I would love to learn more about it.

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

April 2014

S M T W T F S
  12345
6789101112
13141516 17 1819
20212223242526
27282930   

Recent Entries

Recent Comments

Tags

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)