I want to create one standalone executable file which accepts some user input and it should use those inputs as arguments to run a command at CMD as background process. For GUI i thought of using swings and to run cmd batch file . Is it possible to merge java files and batch file to form an exe.? or i should go for someother scripting languages to execute command at background process and someother programming languages for UI design ? Thanks in advance.
Theoretically you could do this all in Java. As you mentioned, you could use Swing to read in the user input. Instead of trying to get ahold of the input data from a separate batch file, you could just use Java's Runtime class to execute a command using the inputs as arguments.
And, if you need this all to run from an executable, you could use a utility like Launch4J or JSmooth to wrap the Java application.
Related
I'm currently trying to automate some data export from a monitoring program written in JAVA. I don't have the source code so I got a JAVA decompiler to look inside my .jar file.
I found the functions that I want to execute but I don't manage to execute them by commands in cmd prompt.
When I launch the .jar file, the main() function starts, it launches two event watcher threads that wait for actions. The actions that I want to perform are normally accessed trough buttons pressing.
I tried launching hese functions directly but my batch lines aren't even read. The only read line is the first one, launching the program, then nothing, it just waits and doesn't read the other lines.
I'm a bit lost and I could sure use some help !
Thanks in advance
I'm trying to build a simple text editor using Scala's swing library and I have to support two side by side windows. I was wondering if it was possible to have the second window be the terminal (bash, Unix). I haven't been able to find any information on the subject. Thank you for any information.
The question is: What is "the terminal"? bash is a shell but you need an implementation of a terminal which runs a certain shell. In general, I would say there are two possibilities:
Find a terminal implementation, which can be used directly in Swing. Maybe you this or that may help.
Implement your own terminal. You may want to start to wrap the shell with a ProcessBuilder. Now you can redirect standard input and output of this process so that you can control it programmatically (more information: here and there). Then you have to create the UI part which (1) reads input from the user and (2) displays the shell output in your window.
I have an eclipse project (let's call it myapp.java) that is a java SWT GUI with a few text boxes and push buttons. When the user enters the required information, it writes a text file and creates a string called command-line . It then uses the run-time command:
Process process = Runtime.getRuntime().exec(Cmdline);
to execute another program, written in C (lets call this myapp.exe). The command line contains myapp with various command line options. This program reads the inputfile.txt and writes and outputfile.txt.
The java program then copies the file outputfile.txt into a window.
Can I make this application into a dynamic web application and place these two programs on a web-site and execute them on the host machine? If so, what are the steps involved? I will need to run an exe file on the host machine from a java program and read and write text files on the same machine.
The cmd executing part will be the same as for SWT. It should be no problem to reuse your existing code, if the SWT GUI and the cmd execution logic are are nicely separated. Of course the GUI (web) part is completely different.
When I try to use java.lang.System.console(), I get a null pointer. I can still write to out and read from in, but this only works when I run straight from my IDE. When I run the .jar file directly, nothing happens. How can I create a console like I'd see using std::cout for use in Java?
Edit:
I was hoping to just create one, rather than understand why I don't have one, since I need one for my program's operation.
Perhaps you're trying to get the console by double-clicking in the jar
Try creating a batch file that opens the console for you.
You can however create a console using Swing and redirect standard input/output there.
Source: Create Java console inside a GUI panel
How are you running the JAR file exactly? That would be the expected behavior for double-clicking its icon in Windows Explorer, as Kelly alluded to, but not for firing it up from the command line.
From the Console entry in the API (emphasis mine):
Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.
java.lang.System.out and java.lang.System.in are the input/output streams for console access. Java won't create a "console" but allows you to interact with the IO streams provided by the operating system.
When you run it from a jar file (like clicking on it from a folder) you'll get the I/O streams of the GUI which don't display anywhere.
Try creating a batch file with a 'java -jar ' command in it. When you run the batch file, you should see the command window. I'm assuming windows here. Another way is to run cmd.exe directly with arguments that keep the window open, i.e. "cmd.exe /c".
Instead of running the jar file directly, open a console (you didn't specify an operating system, but this would be the command line on Windows and a console on *Nix, or Terminal on OS X). Then, run java -jar /path/to/your.jar.
The equivalent of std::cout in Java would be System.out as you probably already know.
EDIT: With regards to your edit, there is code out there to do this. For example, you can use Swing. There's even a StackOverflow answer with more than one working code sample.
See JConsole, which is a general console in java, used for instance by groovy. Or see groovy directly.
Is it possible to start other application that are installed on the system with my java app and pass a file as a parameter to them? I have a client which receives videos from a server and I want my client program to start, lets say the VLC player with the file that I received. How do I manage to do that?
Use Desktop#open(). It will launch the platform default associated application to open the given file.
File file = new File("/absolute/path/to/file.vlc");
Desktop.getDesktop().open(file);
No need to hassle with Runtime#exec() or ProcessBuilder for which you would have to add platform detection and to write platform specific logics for.
Quite simply:
Runtime.getRuntime().exec("vlc [arguments]"); //Write all arguments as you would in your shell.
Make sure you catch all relevant exceptions
You can call the exec method on the Runtime object.
Runtime.getRuntime().exec("System specific command line text here");
You can run an external program pretty easily on Java 5+ with ProcessBuilder, including passing arguments and handling input/output streams.
eg.
ProcessBuilder movieProcess = new ProcessBuilder("/path/to/movieplayer", "/path/to.moviefile");
movieProcess.start();
Only used it myself executing non-UI stuff, I'll give it a quick go and see what happens with something like VLC.
Update - works a treat for flv on Ubuntu, UI is visible and accepts file arguments.