Sure. In various ways. Let's say, for example purposes, that the API you're interacting with is called 'twotter'.
in a properties file
- Make a file. Probably
twotter-auth.properties
. Put:
username: foobar
password: correct battery horse staple
inside it.
- __add it to
.gitignore
- very important. Edit the .gitignore
file (or create it if it does not exist yet) and add:
/twotter-auth.properties
to it. This file should be in the same directory as the twotter-auth file. This ensures that it won't get checked into version control.
- In the java code, read it using the
java.util.Properties
class.
Variations on the theme: There are a bazillion 'properties file' style libraries - JSON, YAML, TOML, and more. Pick whichever one you like.
args
You can also pass the username/pass as command line arguments - they will arrive in the args
in public static void main(String[] args) {
. In your main method, save them someplace so that your API client can access them. Then, in eclipse, click on the arrow thing next to the bug icon, pick 'debug configurations...' from the popdown menu that shows up, pick your app from the list on the left (or click on the 'new configuration' button, it's the leftmost one on the buttonbar in this dialog, and pick a main class). In the tab 'arguments', in the box for "program arguments", type whatever you want.
VM properties
Very similar, but instead of as program args, you can pass VM sysvars. In the same place as the 'args' choice (Debug, tab "program arguments"), there's a textbox for VM args. You'd type in something like:
-Dtwotter.username=foobar "-Dtwotter.password=correct battery horse staple"
from java code you can call System.getProperty("twotter.username")
to access these. Note that you can, under the tab 'common', choose "Shared file" in the "Save as" section, which creates a targetname.launch
file in your project. That file WILL contain all this info, so unless you .gitignore
it, you might check that in, in which case you've just checked in username/password data. Keep it on local
, though now you need to explain to everybody else how to make this thing (whereas if you save it as a shared file you can check that in and now everybody that also uses eclipse gets this run target, all configured up properly, out-of-the-box, which can be nice).
environment variables
You can also make a system (as in, your OS: Windows, linux, etc) variable. Same place JAVA_HOME and PATH exist. You can read these from java code with System.getenv
.