I have three classes which all make different works but I need to run them together. When I run all of them in one class I just get one console and cant change this console. What I want is to run them in one class and see each console . How can I do that ?
To be more clear ,
when I run first class, I get --> console 1
when I run secondclass, I get --> console 2
when I run third class, I get --> console 3
but instead of doing it seperatly, when I do it;
Run 1,2,3 ----> I get console 1
The thing I want is
Run 1,2,3 ----> I get console 1, 2 and 3
Thank you all
EDIT 1 : Sorry for not-enough info I use Eclipse to run my code, and I am talking about Eclipse console.
A certain way to do it would be to open 3 separate terminals and do java Class<n> (without .class extension) in each terminal.
I don't think you can do it in Eclipse with a single workspace (it could be possible though ...), one way to do it is to create separate projects for each class and open each project in a separate workspace simultaneously, but this is too much work IMO. 1. is probably the easiest/fastest way to do it.
You have to invoke java command separately for each class in different console. With one java invocation, i think, there will be only one console associated.
Obscure question. But it seems that you need separate console outputs for your classes. I don't think it is possible. That is operating system restriction. If operating system supported that you could somehow create additional consoles.
As a solution you can give your classes PrintStream object which they will use to output data.
You could run a server-like application which would accept messages and then display them in the console. The class would start each of your other main methods in new Threads, and then wait indefinitely for messages, ending when the other three threads close.
INMO, if your underlying OS is Windows then you need to handle console and use JNA or JNI that allows you to call win32APIs from java, but if your underlying OS is Unix-like then you need to find a way to communicate with syscall APIs in java. by calling native APIs you can call native console and if you know how to create a native console then you can create multiple instances of native console.
Related
I'm creating a simple notepad (just like a clone of the real one). As an advancement I decided to add a Java compiler and Runner in the app making it a very basic IDE. My problem is how to I tell Java that System.out.prinltn() should print it's results in my own console?
I know the question is short but this is what i wanted to know. Not only for System.out.prinltn() but for all the other things which are printed in the console.
Thanks in advance
You need to pass arguments to the your notepad application.
For that you need to use windows controllers to access your notepad application from another java application.
IF both of the application written in java then you can use TCP ip at the backrground to communicate. This is the only one way, there are alot of other ways.
how to run a jar file in background in windows cmd
this below jar file cmd i want to run in background
java -Djavax.net.ssl.trustStore=cacerts_appedo_agent-Djavax.net.ssl.trustStorePassword=changeit -jar appedo_tomcat_agent_2.0.063.jar
You can't do this in a platform-independent manner.
In Unix/Linux, you would call the fork() system call, which duplicates your process. With Java, that means duplicating the entire JVM. Then, you'd have to figure out whether it's the parent process or the child process, which you can determine from the process ID that the fork() call gives you. If it's the parent process, you exit. If it's the child process, you have to close standard input, standard output and standard error.
In Windows, there appears to be the FreeConsole function, but I know next to nothing about Windows programming.
So conceivably, you could write a JNA library that figures out on which platform you are, and invokes the appropriate calls to achieve this. But it's probably not the best idea to get rid of the console window when starting a Java application.
Use javaw, although you won't get any console output either, which is inconvenient.
I have a java application that runs multiple threads where most of them produce unique output that can't and shouldn't be mixed with the output of other's.
In a nutshell I'm wondering if there is anything available that allows me to "switch" inbetween the output of the different threads. For example "Press x to navigate to the output of the next thread"
The linux screen command is basically what comes close to this.
Needs to work in commandline so no swing available.
Any input is highly appreciated.
If not it would end up in a custom build....
If it's only about viewing logs then Andreas's answer would be your best option.
If the actual need is more involved than that, and you wand to build a text-based GUI, check out Lanterna or other suggestions discussed here.
Write the output to separate log files. Now you can tail -f whichever log file you want to view. You can even open multiple command windows and tail multiple logs in parallel.
From what I read, there are a couple of ways to run java files in a node.js application. One way is to spawn a child process: (the java code is packaged with dependencies in an executable jar.)
var exec = require('child_process').exec, child;
child = exec('java -jar file.jar arg1 arg2',
function (error, stdout, stderr){
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null){
console.log('exec error: ' + error);
}
});
The other way is to use the java - npm module (link), a wrapper over JNI (this will let me create objects, set and get attributes, run methods).
In a production environment, when I want my node.js (Express) server to call a java program (it just saves an image to the local directory), please advise me on which would be the better way to accomplish this (in terms of best practices). Also, there is a long list of arguments that I need to pass to the main class and doing that on the command line is a bit of a struggle. Should I make the java program read from an input file instead?
1) If you use exec, you will run an entire program, whereas if you use a JNI interface, you'll be able to directly interact with the libraries and classes in the jar and do things like call a single function or create an instance of a class. However, if you don't need anything like that, I think using exec is far simpler and will also run faster. Sounds like you just want to run the Java application as a standalone process, and just log whether the application finished successfully or with errors. I'd say it's probably better to just use exec for that. Executing a child process this way is also far better for debugging, debugging JNI errors can be very difficult sometimes.
2) As for whether or not to read arguments from a file, yes, it's usually better to read from some sort of file as opposed to passing in arguments directly. It's less prone to human error (ie. typing in arguments every time), and far more configurable. If someone like a QA engineer only needs to edit a config file to swap out options, they don't need to understand your entire codebase to test it. Personally I use config files for every Java program I write.
You can use deployment toolkit and run the jar through jnlp. https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html
Advantage of running jars through jnlp is the ability to pass parameters from javascript to your jar. In this way you can dynamically customize your java program.
For this kind of problem you'd want to approach it in the following way:
Is there a decent way to run processes with arguments in my language/framework
Is there a decent way to deal with the programs output?
From experience, a decent way to deal with arguments in a process is to pass them as an (string) array. This is advantageous in that you do not have to resort to unnecessary string interpolation and manipulation. It is also more readable too which is a plus in this problem setting.
A decent way to deal with output is to use a listener/event based model. This way, you respond appropriately to the events instead of having if blocks for stderr and stdout. Again, this makes things readable and let's you handle output in a more maintainable manner.
If you go a bit further into this, you will also have to solve a problem of how to inject environment variables into your target program. As an example, you might want to run the java with a debugger or with less memory in the future, so your solution would also need to cater for this.
This is just one way of solving this kind of problem. If node is your platform, then have a look at Child Process which supports all of these techniques.
We can run the whole java project by making .jar file of it and run it using the command in the shell and run that shell file. In order to run java code from nodejs project as we know project could be a mix of java, js modules.
Call exec() function in node to create a child process to execute the shell file having a command to run .sh file and can also pass some argument in it from use.eg;
let fileName = 'someFile.txt';
let userName = 'Charlie Angle';
exec(`sh run.sh --context_param
paramFilePath="./storage/${fileName}" --context_param userName="${userName}"`, (error, stdout, stderr) => {// Some code based on execution of above command})
You can simply call a java command , with classpath & arguments, using module node-java-caller, it embeds the call to spawn and will also automatically install java if not present on the system
https://github.com/nvuillam/node-java-caller
Q. Is there a way to have a java program run twice on a mac like on windows?
You do not specify if you want to start a simple .jar or an application-bundle so I will give examples for both. To run multiple instances of an application-bundle on OS X, you can use the following trick; open the Terminal and start the application with this command:
open -n /path/to/your/java.app
Each time you call this command, a new instance is opened up.
Attention: Just because it is possible to start several instances does not mean it is a good idea to do so. Make sure you will not run into trouble with concurrent write-access of multiple instances with the same file.
If you are trying to run a jar, you can simply call
java -jar /path/to/your/java.jar
several times to start up several instances.
To start up the java-application from inside a java-application under OSX, you have to do something like this:
In the case of a simple jar:
File jarFile = new File("/path/to/your/jarFile.jar");
Runtime.getRuntime().exec(new String[] { "java", "-jar", jarFile.getAbsolutePath() });
In the case of an application bundle:
File jarFile = new File("/path/to/your/jarFile.app");
final String[] command = { "open", "-n", jarFile.getAbsolutePath() };
Runtime.getRuntime().exec(command);
I don't really understand the problem. But why don't you abstract it to a method instead of naming it program and call that subroutine twice. Or spawn two threads?
Maybe this is a trick question, but I would open up two terminal windows and run it once on each terminal...
This depends on the nature of your Java program. If your program is running as both server and client, it may cause the problem when your run multiple instances. In many server program, it uses a fixed port number to simplify the setting and implementation. Since a given port number cannot be use by more than one application, you cannot open more than one instance of that application unless you can change the port number in your application settings.
Many Java application uses this trick to prevent user to open multiple instances of their program by checking if a certain port is in use. If this the case, then you cannot run more than one instance of the program.
For other Java application that do not use port or ports do not crash, you can open it twice or more via the terminal.
Assuming that you are running client version of the code on your system and trying to connect to a host. First you need to have the server running on both the machines, B and C in your case. Secondly the client code you are using should be reading the IP address and port to connect. It should not be hard coded or else you will have to change the code and rebuild it for server B. This should help you.
Guess I have answered your query.
:)