accessing static variable in multi threading system - java

I have to take one input from console in first java program. That input i have to pass in second java program which is getting executed as thread from the main method of first java program.
I made the variable as static and tried accessing in second java program but it is showing null value(default value).
I am not supposed to make the object of first program also.
Please suggest me how to do?

If your first java program is starting the second using O.S.-like functions that produces the same effect as if you were starting from the console, then you will have a second instance of the JVM, with everything "reseted". That's why you're getting that null.
A suggestion would be passing the value as a parameter for the second program.

As far as I know there's no shared memory mechanism implemented in Java. Pipes also don't work across VM boundaries, so you would have to use the JNI and C code to create a mechanism.
But then if the two programs cooperate that close together, why not let them in one VM and use threads? Security reasons is the only thing I could think of.

Related

How to run a java class only once in background, in a feature file

I have a statement in the background of a feature file as,
def token = Java.type("Calling a static method from a java class")
I have 4 scenarios in my feature file and this java class is called 4 times. How to call this java class only once?
According to karate docs you have to use callonce
Variables set using def in the Background will be re-set before every Scenario. If you are looking for a way to do something only once
per Feature, take a look at callonce. On the other hand, if you are
expecting a variable in the Background to be modified by one Scenario
so that later ones can see the updated value - that is not how you
should think of them, and you should combine your 'flow' into one
scenario. Keep in mind that you should be able to comment-out a
Scenario or skip some via tags without impacting any others. Note that
the parallel runner will run Scenario-s in parallel, which means they
can run in any order.
Please check
https://github.com/intuit/karate#script-structure
https://github.com/intuit/karate#callonce
https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/callonce/call-once.feature
Background:
karate.callSingle('called.feature#scenarioToBeCalledOnceInBackground');

Java interpreter code at runtime

We have discussed how java is first compiled into Java bytecode and then interpreted by the JVM. Build into the program we are using (Dr Java), there is a panel called Interactions where you can type code in real time and have it be interpreted and ran (I believe that is how it works). I was wondering if it was possible to have a compiled program in java be ran, and then allow a user to input java code to be interpreted to modify the things that happen. I can't really think of any practical uses of this, but here is an example to clarify:
User runs a program and an integer in initialized with the value of 2 and the name of changeNumber. A pop-up comes up allowing the user to input some java code. They can input something like - "changeNumber = changeNumber + 2;" and have the code execute in real time where if you ended up printing out changeNumber, you would get 4.
This is possible using the Reflection API.
As a side note, I do not understand the downvotes. This is a good and well-written question for a beginner.

Returning values from a java program back to a batch file

In the good old days of C you had int main(...) as the entry function and you could call the executable from a batch file and check %errorlevel% in that batch file which would contain the return value.
In java I compile my java application to something.jar and mark a function like public static void main(String[] rawArgs) as the entry point. I then call java -jar something.jar from the batch file. I can even append command line arguments if I want to.
But now I can't check %errorlevel% since my main function is returning a void.
I suppose this is all perfectly logical given that everything is running in a virtual machine and that is the actual executable rather than something.jar.
I can use System.exit(...) to achieve my original aim.
My question is this: Is there a better way of doing this? Killing off the virtual machine seem heavy handed. What if the code runs server side? Am I missing a cute function like Runtime.SetErrorLevel which does what I want?
System.exit() is the correct way to do what you want - a process can only return an error condition when it exits anyway, so what would be the point in specifying that code anywhere else?
Simply use System.exit() with a non zero parameter to indicate the error level - there's no need to look for an alternative way. If you try to work around it you're just going to end up with horrible native hacks that aren't portable yet accomplish the same thing as System.exit().
Place a call to System.exit() just before the end of your main function.
Declare a native function in java, static native void SetErrorLevel(int level);, generate the JNI headers using javah.exe and implement the function in C which sets the %errorlevel% environment variable in your process.
Then terminate normally.

How to use Java to sent and read a third party javascript variables

I have two javascript files:
http://www.dublinbikes.ie/design/marseille/javascript/gmaps_common.js
http://www.dublinbikes.ie/design/marseille/javascript/gmaps_search_station.js
Which for http://www.dublinbikes.ie/All-Stations/Station-map# website
What I want to do is to simulate the search a station status action that on the left hand side by set variable 'value' a station number and pass it to gmaps_common.js for execute, then read the result from gmaps_search_station.js that executed variable called html from LeftBlock(id) function.
Because I need run whole process automatically several times, so please suggest me could I use Java to do this and how.
Thank you very much for the help.
You should never try to emulate a JavaScript interpreter just to get js variables out of it - just do the same thing the script would do in your language. Java is mighty enough.
The script just gets a list of stations from http://www.dublinbikes.ie/service/carto, and then queries details from http://www.dublinbikes.ie/service/stationdetails/dublin/{number} (example). Java has lots of HTTP and XML libraries, it will be no problem for you to do that, too.

sending text data over network in java

I have a use case where we have two different java programs doing:
First java program has to send some parameters to invoke the second java program.
First java program has to send datastream to the second program.
The approach that I have in mind is:
Use Socket class where second java program is socket server and first java program socket client.
First java program serializes the arguments needed by second java class to initialize it's class object that performs the task. Second java program de-serializes it.
Now the first java program has to send data that second java program on another machine would work on. The First program can send data as a BufferedWriter object but how will server know that it is receving the arguments and after arguments, it has got the stream to work on ?
Any other inputs for the approach are greatly appreciated !
Thanks
check out here, its nicely explained.
http://download.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

Categories

Resources