Writing Java interpreter Plugin - java

A few days back I wrote a Textinterpreter plugin in Eclipse which basically takes a text file and simply printout it's content in the console. It does this by first taking a text file and converts it to a string.
then it makes an Arraylist out of it from which each line is printed out in the console.
List<String> mLines = new LinkedList<String>(Arrays.asList(string)
while(!mLines.isEmpty())) {
String line = mLines.remove(0);
if(line.equals("Stop...")){
debug(DebugAction.Suspend);
}
System.out.println(">>> " + line + " <<<");
}
You can see an if statement in code above which checks whether "Stop..." is written on any line in the text file and if it is then the debug() funtion is called(which suspends running unless the user press resume() button in debugmode.)
Now I want to do the same for .java files. i.e write a Java interpreter plugin which execute a java file normally until it finds "Stop..." written in code.
Any Suggestions?

I don't think you really want to implement a Java interpreter, that'd be a huge project keeping you busy for some years maybe.
The most natural solution for you task might be to scan the Java source file and automatically create breakpoints at each Stop statement. Then run the application in debug mode and you get the desired behaviour. Since you only need the line number for creating the breakpoint you can actually keep reading/scanning files line-by-line.
To get additional statements executed (like calling debug(..)) add your snippet as a breakpoint condition (followed by return true; to tell the debug to stop indeed).

Related

How to grab CMD input with line that updates

I'm sure some people are familiar with the concept. You run a command at the CLI and as it progresses, the one line in front of you updates with a percentage. Under the bonnet, before each line is printed, it clears the current line, so instead of 100 lines of progress updates, you just have one line which updates until it hits 100%.
I want to make an app that will echo this information out into an embeded diaplay window. I'm fairly new to Java and I'd like my app to run a Windows command (sfc /scannow to be exact), display the output to the user on-the-fly and then once complete, I want to analyse the log file and give a readable conclusion to the user.
Is there some kind of library or special way of going about doing this so that when the line is "updated" in the console window, I can simply overwrite the current line in my display window?
The line is probably updated by the program writing \r control character (carriage return) to stdout. It is the task of the receiving application to handle this. The normal terminal handles this by setting the cursor to the start of the line. Your program can intercept this character and also treat it by resetting the line.

Read from console data previously "System.out.println()" possible? [JAVA]

This is not the basic Scanner(System.in).
I have some recursive methods that I saw online that does exactly what I want but now I need to count the number of lines that have been "outputed" to the console.
So, I run the program. It does some computations and then it writes on the console some lines (as you can see above) and I need to , in the end on the computations (after the execution of the main), count the number of that lines and write it to somewhere.
Example of the output:
Does anyone know how it can be done?
Thanks.
You could use the class TeeOutputStream from Apache Common IO that split the outputs into two streams. This way you can still publish your stream in the default output and log them in a file. With the outputs in the file you can do whatever you wanted (count lines...).
Why don´t you declare a variable counter and initialize it with 0. After every System.out.println() statement, you increment the counter by adding counter++.
As final Statement in your main-Method, you print the variable to the console:
System.out.println("Counter: "+counter");
If that doesn´t work for you, you could still write the output of the program to a file instead of the console, read that file afterwards and look for line breaks.

Java CLI App - keep input from scrolling up

I get that this isn't possible to do with normal java, although if there are any libraries out this it would be very useful.
Essentially, I'm designing a console app and running into an issue that when output happens while something is typed in the input line, that input text will move up and appear before the line that just got output. Is it possible to fix this in some form so that the text you are inputting that stays at the bottom?
EX:
I'm typing something as input into my commandline app, and then the program prints something WHILE I'm typing - this causes what was originally on the input line to be scrolled up with whatever the output text was. When you are trying to type something in this can obviously be detrimental. I know it's possible to prevent this.. (Other programs have done it... EX: Minecraft Server)
(If I need to be more descriptive I can.)
You could use the help of threads. One that listens to user input, the other process the actual output. This problem is similar to basic race condition problems when multiple threads attempt to read and write to a shared resource.
Your shared resource is that console. You need to keep the Input/Output operations synchronized. Have a look at race condition.

reading a file from last line

I would like to read a file from last line using RandomAccessFile. Is this possible or do I have to use another class?
Beside this file changes during the time so the last line doesn't remain last forever. During the reading another, java program write on it. My question is: the program will see in the same time another java program write on the file, the changes?
Edit
Well suppose I have a server that write its faults in a error log file during it's running.another program reads every line.which should be the best way?
Yes reading a file from the bottom up is possible using RandomAccessFile:
Reading the Last Line of a File in Java through Random Access
as for the other part of your question:
Beside this file changes during the time so the last line doesn't
remain last forever.During the reading another java program write on
it.My question is: the program will see in the same time another java
program write on the file, the changes?
I would propose a SSCCE in which you show what you are trying to accomplish and the problem
EDIT:
As Jon Skeets comment suggests, I found a link to a similar question answered by him: Quickly read the last line of a text file?
EDIT 2:
I think I got your second question, I'm not sure it's possible, as a single file cant be accessed by 2 different streams at the same time, one will just throw an error when trying to open the file. Ypu can however monitor if changes occur after the file has been read using Java.NIO Directory Watcher, Unless I misunderstood you.

logging to swing application and console + writing to a text file the result.

I have a program that can run either by a Swing Application or a Command-Line
My question is this: How can I avoid the following code:
String message = "performing task #1; data abcd";
System.out.println(message);
jTextArea.append(message);
stringBuffer.append(message);
I need those 3 lines for the following reasons:
line 1 - so I can see the console output (I use eclipse)
line 2 - so I can see the output on the swing application
line 3 - at the end, I write all the log into a file.txt
is this something that can be achieved in ONE line? (not sure but maybe a log4j?!?!, if so how?)
Thanks!
Well, just making an assumption here: the place in which you want 1 line and not 3 is where you have the line you want to write to (up to) 3 places. So why not write your own method; you can make it static and even have it test which kind of logging to do based on application properties or whatever, then your code with the message can invoke it as:
Logger.logMessage(message)
And IT decides how many places to write it to.

Categories