Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7413

How to access static variable of another program

I want to make two programs. where program 1 will have a static collection and some getter/setter to access/update its values.

I want that program 2 should be able to access/call getter/setter of program1. so that static collection can be shared among many programs/process

*i dont want to engage any port.

Upvotes: 1

Views: 1028

Answers (3)

JohnGray
JohnGray

Reputation: 676

There is no strait way to do this. RMI or CORBA should work. But it will be an overkill. You may use plain old sockets to communicate between Java apps. Or use java.nio channels.

Upvotes: 1

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

This cannot be done just with static variables. They are accessable everywhere inside the JVM your program run in, but can't be accessed that simple. Use RMI, or sockets, or input streams to handle this interprocess communication.

Upvotes: 1

tdammers
tdammers

Reputation: 20721

You can't just declare a variable static (or super-static) and expect it to be available in code outside of your program - it just doesn't work that way. What you need is some sort of inter-process communication, and the possibilities are endless. To name a few: - serialize / deserialize to and from a file (local or on the network) - sockets (basically, you open a network connection between two ports on localhost) - a database - shared memory (whether this is possible depends on the OS) Your OS of choice may offer other means, but the principle remains the same: whenever the variable changes, one application needs to notify the other.

Upvotes: 6

Related Questions