Open() in Processing Code Not Working - java

So I asked this question concerning how to combine 3 different applications into 1 Combining Processing Applications into 1 Big Executable?
From lurking I have learned the following methods I could go about to do this:
use Java and embed my sketches in a Swing interface (rewrite
programs into some complicated Java monstrosity)
Rewitre code to launch separate windows using G4P
http://www.lagers.org.uk/g4p/ex-windows/index.html (seems better
than option 1 but still a pain in the ass because trying to write
functions to get the LEAP motion api calls to work in the separate
windows wont be easy)
Use Open() at the end of each program to run
the next program (quick and dirty, just the way I like it!)
Combine all 3 into a batch file. (I dont really understand how to go about doing this but seems similar to previous option).
Now theoretically I could get more functionality from 1 and 2 (in terms of program flow and better user interface) and would probably learn more and be better off in my future efforts, however time is of the essence at the moment. Each of the programs creates csv data files from user input that I will use to create a visualization so the manner in which I get the programs to run is somewhat irrelevant at this point. Only problem is I cant get open() to work correctly. See below:
void setup() {
size(200, 200);
}
void draw() {
// draw() must be present for mousePressed() to work
}
void mousePressed() {
open("C:/Users/corbett2/Documents/Processing/test/application.windows64/test.bat"); //doesn't work
open(new String[] { "cmd", "/c", "start", "/w", "C:/Users/corbett2/Documents/Processing/test/application.windows64/test.bat"}); //also doesn't work
}
I've tried a bunch of different ways to use open() but it wont run the program. I'm using windows 8. I exported the application "test" which created the "test.bat" located in C:/Users/corbett2/Documents/Processing/test/application.windows64. I believe you have to export your sketch before you can try to use open() on it right? As said before, the idea here is to use a call to open() at the close of each of my 3 applications in order to run them.
So my specific questions are as follows:
Which of the 4 methods I listed would you recommend I pursue for my issue? If none, please feel free to suggest something else.
Will option 3 using open() work? Why or why not?
Correct my open() issue.
Thanks in advance!

When I export application it creates test.exe for windows32 bit architecture (but running on 64bit OS) and then using your second case of open() works fine for me:
void mousePressed() {
//open second window with test app running
open("C:/Users/corbett2/Documents/Processing/test/application.windows32/test.exe");
//this will close first window
exit();
}
[but this didn't work with export to 64bit architecture (for me) so your problem should be with some settings of platform's launcher]
Anyway this approach works fine just you also need to close first window so user will not be confused.

Related

Implement Command History within Java Program?

I need some coding advice. I've written a simple Java program which runs within one of my company's internal websites. Its a command line program where users enter commands like copy file1 to remote1 and compare file1 to file2 archive diff and so on. My colleagues like it and use it frequently.
The problem is the users' commands tend to be long and repetitive. They have asked me if it is possible to implement a command history in the program. This way, they can use the up arrow or something to scroll through, edit, and resubmit previous commands. There's no need to remember commands entered in previous sessions, only the current session.
I've thought about it, and I think I could come up with a solution from scratch... but it would take a few weeks to develop. I'd much rather implement an available package or module, if one exists and isn't too much trouble. I know this is an open-ended question, but can anyone recommend such a resource?
I don't see why it should take so long to develop. Here's a rudimentary solution:
class CommandSession {
private List<Command> commands = new ArrayList<>();
private ListIterator<Command> scroller;
public void execute(Command command) {
scroller = null;
commands.add(command);
command.execute();
}
public Command scrollUp() {
if (scroller == null) {
scroller = commands.listIterator(commands.size());
}
if (scroller.hasPrevious()) {
return scroller.previous();
}
return null;
}
}
You could tweak this in various ways for more advanced functionality, but overall it's a pretty basic concept.
if you are using a *nix environment then rlwrap may be what's you're looking for.
rlwrap tries to be completely transparent - you (or your shell) shouldn't notice any difference between command and rlwrap command -
except the added readline functionality, of course. This should even
hold true when you are re-directing, piping and sending signals from
and to command, or when command manipulates its terminal settings.
There are many options to add (programmable) completion, handle
multi-line input, colour and re-write prompts. If you don't need them
(and you probably don't), you can skip the rest of this manpage.
if not, you can use cygwin to be able to use it
I've used CRaSH in my project. It's basically a SSH shell that the user is expected to connect to (supports username/password too) and yes, supports history in commands. Commands are written in Groovy.

Differences between SWT/JFCE in Linux and OS X

I am running in the following:
I have an eclipse plugin(Luna/JVM 1.8) that presents some information to the user in a CTabItem. In this CTabItem, I have a class that updates a TextViewer's widget contents using asyncExec(....).
A brute force approach of getTextWidget().append(String) process the String[] inside the UI/asyncExec();
This logic has worked reliably in OS X (El Capitan). I am using linux (openSuse Leap) now and the updates rarely show up in the TextViewer. I have checked and the runnable inside the asyncExec(...) gets called.
The rest of the UI elements work normally in both OSs.
What do I need to change or am I missing? This seems more due to my lack of understanding of cross-platform SWT than anything else.
Thanks a lot!
I made 2 simple changes:
Instead of calling getTextWidget().append(String) I am using a StringBuilder and making just one call to getTextWidget().append(String).
After this I call refresh().
These two did the trick.

Ways to Clear the Console?

I have been on this for a while now, and for the past three days have ripped apart the Internet for ways to effectively clear the console in Java.
Ways I have seen it "done"
This way
for(int x = 0; x!=100; x++){
System.out.println();
} Sucks, as you can just scroll up and see the printed statements again.
Console.Clear(); and all variations of it, have not worked for me.
Runtime.getRuntime().exec("cls"); has not worked in any cases i have tried to use it in.
(I use JCreator to code, I have no idea if this has anything to do with my issue)
This way by joesumbody122, looked interesting:
private static void clearLine()
{
Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));
}
and
private static void clearLine(int left, int top)
{
int pLeft = Console.CursorLeft;
int pTop = Console.CursorTop;
Console.setCursorPosition(left, top);
Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));
Console.setCursorPosition(pLeft, pTop);
}
But sadly i could not get it to work for me. It gave me errors that all the methods that he called from Console did not exist. java.io.*; was imported His method clears one line specifically, so if someone could get this working, (Again, I use JCreator to code, I have no idea if this has anything to do with my issue) I could see it being looped to clear all the lines.
Ways to make less sucky?
Back to this for(int x = 0; x!=100; x++){
System.out.println();
} Is there a way to prevent the user from scrolling up in the command line? To set the cursor to the top left of the prompt? That would make this method a whole lot more useful.
Another Theory
Is there a way to simply tell java to stop printing in one command line, start printing in another, and close the window of the first so it only appears to have cleared the console, but instead created an entirely new one? I have pondered this the last two hours, and in theory it would work, but i don't know if the code to do so even exists.
There is no reliable way that works everywhere. You've already mostly discovered this.
Generally, command-line output goes into a terminal scrollback buffer, of which only the last n lines are displayed, but previous lines are available through a scrolling mechanism. A command-line program does not write directly to this buffer, but writes to stdout which is, in most cases, piped to the terminal process which then displays the data.
Some terminal programs (i.e. those supporting ANSI escapes) might let you clear the visible portion of the screen. As far as I know, only Windows' cmd.exe responds to a 'clear screen' request by clearing the entire scrollback buffer. On Linux AFAIK it's not possible to discard the buffer completely. And, on Windows, cls is not an executable command but a shell builtin, so you cannot run it from Java System.exec().
Also, any command-line program can have its output redirected to a file with out the program being aware of it, in which case 'clear screen' doesn't have much meaning.
If you MUST have this level of control then you will have to write your own display window using Swing, AWT or GWT or whatever and manage all interaction there.
If it's a command-line app and the terminal/shell you're running the app in supports ANSI escape codes then you can just do this:
System.out.print("\033[H\033[2J");
System.out.flush();
The answer depends on weather or not you are using Linux or Windows OS. If you are using linux and want to clear the console then try:
try {
new ProcessBuilder("/usr/bin/clear").inheritIO().start().waitFor();
} catch(Exception e) {}
If you are using windows try:
try {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} catch(Exception e) {}
You can handle the exception any way you want.It does not matter becasue you will not get one.

How to Clear Console in Java?

I have been pulling my hair for this since quite long time. I have researched for an hour on how to clear a console in Java.
All I found was dirty hacking either by printing a bunch of lines or executing this
Runtime.getruntime.exec("cls/clear");
However, nothing seems to be working for me. Isn't there really any a way of clearing the console in Java like in C (clrscr();). Isn't there any external library by which this can be achieved.
Please let me know if anyone has ever done this before using a proper function, library etc. instead of dirty hacking.
If your terminal supports ANSI escape codes, this clears the screen and moves the cursor to the first row, first column:
System.out.print("\033[H\033[2J");
System.out.flush();
This works on almost all UNIX terminals and terminal emulators. The Windows cmd.exe does not interprete ANSI escape codes.
Try this code
import java.io.IOException;
public class CLS {
public static void main(String... arg) throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
}
Now when the Java process is connected to a console, it will clear the console.
Runtime.getRuntime().exec("PlatformDepedentCode");
You need to replace "PlatformDependentCode" with your platform's clear console command.
The exec() method executes the command you entered as the argument, just as if it is entered in the console.
In Windows you would write it as Runtime.getRuntime().exec("cls");.
Use the following code:
System.out.println("\f");
'\f' is an escape sequence which represents FormFeed. This is what I have used in my projects to clear the console. This is simpler than the other codes, I guess.
You need to instruct the console to clear.
For serial terminals this was typically done through so called "escape sequences", where notably the vt100 set has become very commonly supported (and its close ANSI-cousin).
Windows has traditionally not supported such sequences "out-of-the-box" but relied on API-calls to do these things. For DOS-based versions of Windows, however, the ANSI.SYS driver could be installed to provide such support.
So if you are under Windows, you need to interact with the appropriate Windows API. I do not believe the standard Java runtime library contains code to do so.
You can easily implement clrscr() using simple for loop printing "\b".
If you are using windows and are interested in clearing the screen before running the program, you can compile the file call it from a .bat file.
for example:
cls
java "what ever the name of the compiles class is"
Save as "etc".bat and then running by calling it in the command prompt or double clicking the file

How to access a matlab function from java code?

My java program uses matlab code packaged as jar files for image processing. The problem is when I call a function(written by me) with a call to 'mmreader' for the first time, it works fine. However any subsequent call to a function(same or different) calling 'mmreader' doesn't work and I get an error stating function mmreader not found.
I am also facing a similar problem in another part of my application where the call to matlab function simply throws an exception, the same piece of code works fine in other files.
try{
vplayer.playmov(player_params);
}
catch(Exception e){
System.out.println("error playing cluster");
}
I would take a quick look at this link Accessing Matlab from Java
Here are a couple excerpts from the page that might be useful.
mlapp.MLApp mlApp = new mlapp.MLApp();
String result = mlApp.execute("a = [1 2 3 4; 5 6 7 8;]");
System.out.println("Execute result is " + result);
Inside those quotes, you can even call a MATLAB function, just make sure that you are assigning the output to the right data type.
The most important part of that link is;
We assume you have downloaded and expanded the J-Integra® kit from http://j-integra.intrinsyc.com/ and installed it correctly.
Without that installed, you cannot do the above statement.
Besides that, in MATLAB you can create a JAR with your functions and sign that JAR to only work with your applications. Perhaps you'll need the MATLAB runtime to make that app work I'm not sure about that.

Categories

Resources