We all mostly use System.out.println in the Console of our IDE. I am using Eclipse.
I can also clearly see the println() message on my Mac's Console app. Which is nice for my personal things.
And here is the code:
public class Main {
public static void main(String[] args) {
System.out.println("Is this logged anywhere?");
}
}
And here's what I see on my Mac:
Does Windows have something similar to the Mac's version of Console?
Sadly, as previously said, we don't really have that on Windows. Your best options is to run your program from CMD and then pipe the standard out to a file. Something like java -jar HelloWorld.jar > hello.txt.
What I usually do is create a Handler to a log file (usually just [program name] log [date].txt and have all messages outputted there depending on the log level, which is good practice if you're used to only using System out prints.
Straightforward answer remains that no such functionality comes standard with Windows. You'll have to pipe to a file in some manner.
It does not. However you can simply run the process from a command prompt if you want to examine its stdout. (As you said, eclipse works too, of course.)
Related
I've used Amazon corretto and Java's jre(javaw.exe) to try running my jar file. The task manager says that 'Java Platform SE Binary' is running but no dialogue box(or display) of the jar program is shown on my laptop.
I even tried opening it through command prompt or through a bat file, all in vain.
Any and all help would be deeply appreciated! Thanks! Task Manager snap
A java app does.. what the java app does.
Which may well involve no GUI whatsoever. You won't notice anything whatsoever in your windows environment if the java app you're trying to start doesn't actively involve any GUI elements from e.g. the javax.swing package.
Try using java.exe - javaw.exe does not show any console input or output, whereas java.exe does. If it's a console app (that reads and prints text from the command line), it would simply appear to be doing nothing if you try to launch it with javaw.exe.
For example, this app:
// Save this as 'Example.java'
public class Example {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
then open a 'dos box' (run cmd.exe), navigate to the proper directory, and run:
javac Example.java
javaw Example
does nothing. You witness precisely what you are witnessing (or possibly you can't witness it as the app closes too fast. But you certainly won't see any windows or any text). Then run:
java Example
and you'll see: Hello, World! and then the app exits.
If 'the command line' is gobbledygook to you, well, if you want to program, you're going to have to know how it works, but, fortunately, there are plenty of tutorials around :)
I'm on Linux and I have a Java application (JAR archive) which is using exec() to do it's stuff. I need to find out which commands is that application exec()uting... I tried strace and jstack but without any results. Suppose that the app is calling exec("ls"), can I find that ls command just by grepping output of above programs?
So the question is:
Is there a simple way to watch what is Java application executing with exec() ?
Edit for better situation overview:
Suppose that in Java app i have a button with onclick listener which calls static function from another class.In that function is exec("ls"); called.
When I click that button I see this in strace:
futex(0x7f14a6f799d0, FUTEX_WAIT, 4968, NULLDownload button clicked !
Trying SCP FROM...
<unfinished ...>
Trying SCP FROM.. is just my sout in that button handler right before calling exec().
Another edit:
Thank you guys, but I'm talking from OS point of view... Suppose that I'm sysadmin and I downloaded JAR. I want to know (from outside) what is that JAR doing - I'm only interested in programs started from exec()
So I tried strace but it shows nothing about calling that command from exec... Maybe it is logging too much low level calls for this...
Then i tried jstack -m but I can't find anything looking like that command from exec. I tried grepping string but with no luck.
Ok, what I'm going to propose is a veeeeeeeeeery rudimentary way of doing things, but it might be what you are looking for.
As you probably know, a .jar file is just a ZIP archive comprised of Java .class files. If you just need to get a peek at which commands are going to be executed, and if you know the class that is supposed to execute them, you can just extract the class files from the jar file with gzip and then use strings on them to look for commands.
For example, here's the most simple class I could think of that uses exec():
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
Runtime.getRuntime().exec("/bin/ls");
} catch (IOException ignored) {}
}
}
If you do strings Main.class you should get something like this:
[...]$ strings Main.class
<init>
Code
LineNumberTable
LocalVariableTable
this
LMain;
main
([Ljava/lang/String;)V
args
[Ljava/lang/String;
StackMapTable
SourceFile
Main.java
/bin/ls
java/io/IOException
Main
java/lang/Object
java/lang/Runtime
getRuntime
()Ljava/lang/Runtime;
exec
'(Ljava/lang/String;)Ljava/lang/Process;
As you can see, /bin/ls can be identified as a string. This should work in most cases, unless your Java program is constructing commands in a weird way, like using a char array to create command strings during runtime just to obscure the commands being executed (in which case I'd be highly suspicious of such a program).
However, if you want to see the commands executing in real time, I'm afraid you'll need to resort to some monitoring utility, since most commands would be too short-lived to even appear on top and the like.
EDIT: Regarding strace: I had a look at Java's native C code for UNIX systems and it seems that it actually uses the execvpe() system call to run all commands launched with Runtime.exec():
execvpe(argv[0], argv, envv);
So, in theory, you should be able to run strace -e execvpe <java command...> to list every command executed (as well as every other call to execvpe() -- you'll need to filter a bit more, that's true).
I'm trying to write a script to rename a computer (among other things) but just can't seem to figure it out. I don't really care what method I use to change the computer as long as i can change it. I found out how to read the computer name by doing
String computername = InetAddress.getLocalHost().getHostName();
However, that doesn't seem to offer any help in setting the computer name. Is there a way to set the Computer Name directly in the java console?
If not, or if anyone has better experience in this area, I also wrote a script using powershell 2.0 that renames the computer. I'm trying to figure out how to run that using
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("file location");
I followed the guide here but when trying to run a test .bat file that should just open the command line I just get this output in the java console:
C:\Users\Stephen\Desktop\opencmd.bat is found
OUTPUT>
OUTPUT>C:\Users\Stephen\workspace\UNM computer rename>cmd.exe
OUTPUT>Microsoft Windows [Version 6.1.7601]
OUTPUT>Copyright (c) 2009 Microsoft Corporation. All rights reserved.
OUTPUT>
It looks like it's just spitting back the command line output into the java console, instead of just running the command and opening the cmd line.
I would really appreciate input as I'm in a crunch for time here, thanks!
First, JDK really does not provide pure java API that allows changing computer name. So you have to run script.
Second, if you want to run script using Runtime you have to provide correct command line. So first try to run your script manually. I believe it accepts a least one parameter (the new computer name). So run it from command prompt and see it is working. Then put it to the working directory of your java program and copy/paste the command line into the java code and see that it is working now. if you want you can read STDOUT of your script and/or get its return code. If you do not care about its output just call process.waitFor() and then get the return code.
Be careful with arguments. Windows computer name may contain unicode characters and spaces. If it contains spaces surround it with quotes. Concerning unicode just try. I hope it will not cause problems to you.
You can also use ProcessBuilder class that allows better and more portable arguments passing.
Good luck.
It looks like opencmd.bat is being executed, so assuming your PowerShell script works, is it possible you don't have administrative privileges?
You can also do it elegantly using JNA, I think this would be the target. But if you are rushed for time, don't bother.
I have a JAVA application that launches (using ProcessBuilder) another JAVA application like this:
String val = "something";
ProcessBuilder processBuilder = new ProcessBuilder("java", "-classpath", dir, appName, val);
Process p = processBuilder.start();
Now, this works fine, appName is launched with the parameter val and it runs and works ... great ... the problem is no Console Window appears ... appName does a LOT of outputting to the console and we need to see it ... how can I start the process with a console?
I am trying stuff like ("CMD.exe", "java", "-classpath", dir, appName, val), etc... but I can't get it right ...
Also, I can't redirect the streams, my program can actually start 5-10 of these appName's, each should have their own console window showing their own information.
Any help would be much appreciated.
Thanks,
console windows are generally not the most reliable form of logging. they only store a set amount of information (buffer) and can behave differently across platforms.
i strongly suggest logging to a file using something like log4j and if you need to see it real time use a tail like program (i see you're using windows).
in addition to this, seeing as you want the windows visible at all times and launching a tail program for each log might be annoying, i'd write my own log window in java swing.
the basic idea is to not rely on the OS too much.
Tried Runtime.getRuntime().exec("cscript java -classpath ..."); ?
Anyway, consider using a logging framwork (log4j, commons-logging), because opening 5 consoles is not the most clever thing to do.
I call a few shell scripts via Process to open a command line window and launch whatever I need. As long as the scripts don't detach - you can usually stop any shell command from doing this -java will still hold the running process.
I did it in linux but the concept should be similar.
#!/bin/bash
# To open a process in a new window.
gnome-terminal -x ./your-real-shell-script-here.sh "$#"
the real script will have your java execution in it, such as:
#!/bin/bash
java -jar your-jar-file.jar "$#"
I think you can use javaw to run on windows, so you might only need the one shell script.
A Console object only exists when you execute java.... from a console. Otherwise, the call to obtain one returns null.
If you want to see a console, you need to open a command shell console (e.g. windows cmd.exe or Unix bash shell window) and type:
java -classpath="..." com.example.appName arg1
If you want to run in a different manner, sorry to say, logging to Console is not for you. Instead, log using one of:
log4j
slf4j
logback
I'm writing a Java Swing Application running on Red Hat Enterprise Linux 5 server that I would like to launch jEdit to view log files.
Here is some example code.
public static void main(String[] args) throws IOException, InterruptedException {
String cmd = "sh -c \"java -jar /tmp/jEdit/jedit.jar /tmp/test.txt\"";
System.out.println(cmd);
Runtime.getRuntime().exec(cmd);
}
The output is:
sh -c "java -jar /tmp/jEdit/jedit.jar /tmp/test.txt"
If I copy and paste the cmd output in a terminal window, it runs fine.
I have tried a bunch of cmd values, but I can never get the jEdit window to be visible.
With changes, this process works fine on Windows.
Is what I'm doing possible on Linux?
Thanks in advance!
As jEdit is implemented in Java, perhaps it would be easier to check the source for what the main method (in the class declared in the manifest file included in the jedit.jar) does and do the same thing without using Runtime.getRuntime().exec() at all.
If you do want to stick with it, you could try passing the individual commands as an array to exec(), this often solved such problems for me.
Linux uses the concept of display ports for its X-Windows system. This allows it to maintain a different desktop environment for each user. It also allows a user on remote machine to run a desktop app from the first machine but see the UI on the remote.
Windows, having only one available desktop environment at a time, does not.
First thing you definitely have to do is add the environment variable "DISPLAY=localhost:0" to the environment from which you are launching this. However, you may also need to run 'xhost +localhost' or this may not be allowed.
Double-check, too, that you didn't successfully launch a bunch of jEdit processes that are now zombies (using top) and kill them if necessary (using kill).
Runtime.exec() needs some special attention. The exec method that accepts a String uses the space character as a delimiter to break up the string into commands. You need to use the exec method that accepts a String[]. Read more here, specifically near the bottom.
I´ve done this once and I got the same problem
What I've done is to write the command line into a text file and then execute the text file as a shell script file.
It worked fine for me.
Jedit has a launcher script, /usr/bin/jedit I guess. Simply typing jedit in command prompt runs it, at least in current version, 4.5. Try that script instead of explicit java command.