How to initialize MPJ Express Outside main method? - java

I'm trying to initialize the MPJ inside a function but I cant make the MPI.Init(args) work. I tried dragging along the args like this
import mpi.*
public class Test {
String [] args
public static void main(String [] args){
Test.args = args;
t.testFunction();
}
public void testFunction() {
MPI.Init(args);
}
}
but I get an exception like this
Exception in thread "AWT-EventQueue-0" mpi.MPIException: Usage: java
MPI conf_file can be,
../conf/xdev.conf OR
http://holly.dsg.port.ac.uk:15000/xdev.conf
at mpi.MPI.Init(MPI.java:232)
The problem is my program works with an action listener on timer interrupts so I cant put it all inside the main method.
Thanks in advance for any solutions or even ideas.

Related

How to send variable between classes while multithreading

This a much simplified version of my multithreading project and its just a way to replicate the issue in a simpler way for easy understanding.
So I have two classes startSession.java and main.java
what I am trying to do is to send a variable from startSession.java to main.java and Im also using multithreading. However, the problem I am facing is that everytime I try to retrieve the variable inside main I get a null value.
Inside startSession theres the run method and Setter(setSessionKey(String sess)) and getter(getSessionKey()) methods. I hardcoded a variable to test.
The get method only works inside the run method but when I call getSessionKey() from inside main I get a null as seen below. However, this is only a problem when I am using multithreading. When I dont use multithreading and instead just call the run method from inside main, the variable Im looking for is no longer null.
My question is there a way to send a variable from startSession to main while using multithreading ?
thank you
startSession.java
public class startSession extends Thread {
static String sessionKey;
public void run() {
String createdSession = "83248329";
setSessionKey(createdSession);
System.out.println("Inside run method: " + getSessionKey());
}
public String getSessionKey() {
return sessionKey;
}
public void setSessionKey(String sess) {
sessionKey = sess;
}
}
main.java
package com.Server;
public class Main {
static String session;
public static void main(String[] args) throws InterruptedException {
startSession startSession = new startSession();
startSession.start();
session = startSession.getSessionKey();
System.out.println("Inside Main: " + session);
}
}
with multithreading
without multithreading
Use a BlockingQueue whereby the Thread (Producer) will add to the shared queue and the Main (Consumer) will block on the take
main
public static void main(String[] args) throws Exception {
// example only uses 1024 - check what is best for you
BlockingQueue queue = new ArrayBlockingQueue(1024);
StartSession producer = new StartSession(queue);
....
System.out.println(queue.take());
startSession
String createdSession= "83248329";
queue.add(createdSession);
see https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/BlockingQueue.html
and
https://jenkov.com/tutorials/java-util-concurrent/blockingqueue.html

How many JVM calling main inside main

class B {
public static void main(String[] args) {
}
}
class A {
public static void main(String[] args) {
B.main(args);
}
}
In the above flow, my init method is A.main which in turn calls B.main.
I know calling A.main will spawn a JVM. Does calling B.main inside A.main spawn another JVM?
OR
B.main is JUST another static method once a JVM is started on A.main as init function.
Option 2. The mains are just static methods of each class, and only one JVM is running when making the call from A to B.main(args).
You can also make use of this in JUNIT tests to help check the command line launch behaves as expected, such as
#Test void coverage() {
A.main(new String[] { "a","b" }); // or B.main
// assertions here if there is some output state you could check
}

How can I invoke a Processing method from outside a Processing sketch?

I want to have a method invokeProcessingMethod(String name, Object... args) that invokes a method from Processing and returns any value that may result.
I already have a method invokeMethod(String name, Object...args) that invokes a method from a superclass on the current instance, so I thought an implementation of this would be create a sketch with the method I already have
class ProcessingRELP extends PApplet{
public static void main(String[] args) {
PApplet.main("ProcessingRELP");
}
void settings(){
}
void setup() {
}
void draw() {
}
invokeProcessingMethod(String name, Object... args) {
invokeMethod(name, args);
}
}
and then do something like
class Test {
public static void main(String[] args) {
ProcessingRELP sketch = new ProcessingRELP();
Object data = sketch.invokeProcessingMethod("textWidth", "hello");
}
}
but I get the following exception because I am not invoking the Processing method in setup or draw
Exception in thread "main" java.lang.NullPointerException
at processing.core.PApplet.textWidth(PApplet.java:12960)
at ProcessingRELP.invokeProcessingMethod(ProcessingRELP.java:27)
at Test.main(Test.java:25)
Is there anyway to invoke a Processing method outside the sketch or a creative way to still do it in the sketch but be able to retrieve the data from outside the sketch?
You need to run the sketch first, because you are trying to invoke methods that depend on the runtime of the sketch (textWidth() uses the textFont and textSize of a live sketch in its calculation) and not static methods. Simply instantiating a ProcessingRELP object will not run it; this can be done with the following:
ProcessingRELP sketch = new ProcessingRELP();
PApplet.runSketch(new String[]{"--location=0,0", ""}, sketch);
Now you are able to call your method since the sketch is running.
I don't know of a way to initialize a Processing sketch in a way that gives you a reference to it. You need to use the PApplet.main() function:
String[] appletArgs = new String[] { "MySketch" };
PApplet.main("ProcessingRELP");
Shameless self-promotion: here is a tutorial on using Processing as a Java library.
On top of that, I would be very suspicious of your invokeMethod() approach. Why can't you just invoke the function directly?
Something like this:
float stringWidth = sketch.textWidth("hello");
Either way, I think you're going to need to refactor your code to use the PApplet.main() function instead of assuming you have a reference to the sketch itself.
You could do something like move your logic into the setup() function of your sketch class. But either way, your Processing sketch needs to be the entry point.

I'm trying to run a program in Java and I don't understand why it's not working

I am trying to run this program in Java:
import java.awt.*;
import javax.swing.*;
public class PanelTest extends JPanel
{public void paintComponent(Graphics g)
{g.setColor(Color.green);
g.drawString("Hello World",30,100);
}
}
When I run it shows a box in which it's written:
No main methods,applets or MIDlets found in file.
I don't understand what do I have to do.
Can someone explain this to me. Any help would be appreciated.Thank you!
Every Java program requires a public static void main (String[] args) as main method. This method is the program's entry point. Everything starts from there.
You can then instantiate the Panel in that main method.
Something like this:
public static void main (String[] args) {
JFrame frame = new JFrame();
frame.setContentPane(new PanelTest());
frame.setVisible();
}
As specified in the error text, the problem is that you do not have a main method for your program. All Java programs must have a main method, since that is the starting point for Java Applications.
A typical main class for your simple application would be:
public class Main {
public static void main(String args[]){
PanelTest pt = new PanelTest();
pt.paintComponent(..);
}
}
In the above, replace the '..' with the actual method arguments. I would definitely going through the basic concepts of Java. There are some great tutorials out there.
Every Java program should be started with a main method:-
public static void main(String[] args) {
// Starting code here
}

after compiling code JFrame does not appear

I'm using the Intellij idea platform.
I have the following code:
package GUI.test;
import javax.swing.*;
public class Ramka extends JFrame{
Ramka(){
setVisible(true);
setSize(100,100);
}
public void main (String[] args){
new Ramka();
}
}
I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?
Also must admit, that I haven't possibility to run method "main". InteligIdea propose me only to compile Ramka.java. After compiling IntelijIdeay says, that compilation completed successfully, but thats all and is nothing happened. In my previous exercises I always ran method "main".
You should do something like this:
public static void main (String[] args){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ramka().setVisible(true);
}
});
}
Besides missing the static identifier at main, you also have to make sure that your frame runs in the right thread check "concurrency" for swing
You would see your Ramka if you actually ran main. Main should always be declared public static void main(String[] args) where you forgot the static. If you change it to:
public static void main (String[] args){
new Ramka();
}
it should run.

Categories