This is written from QA/automated testing perspective, but can be applied to general Java programming.
While the traditional and correct way to develop and debug in Java is using a good IDE with debugger and set breakpoints and watch variables and object references, etc., it is also useful to dynamically work and test with Java at runtime using an interpreter shell session.
You can say I got bit by the “bug” or am spoiled by such feature from Python, Ruby, etc.
I find it useful this way to test thing out quick w/o having to use full featured bloated IDE, setting breakpoints, and monitoring stuff. Just try some code, don’t work try something else from the shell.
This is particularly useful for quick test of code snippets and can be useful in QA for testing Selenium / WebDriver API calls and see if things work or not, or if you got the correct element locator or not.
With Java, you don’t really get an interpreter, but there are some options like interface to Java using Jython, JRuby, BeanShell. Don’t know all the options, but I believe BeanShell gives the closest approximation of using Java in a shell, so this article describes a bit of using it.
As an example of using Beanshell to test/debug/develop WebDriver test code, you can do the following:
Start up Beanshell with reference to Selenium library, I show Windows method, but Linux/Mac OS X method is similar:
java -cp .;selenium-server-standalone-2.15.0.jar;bsh-2.0b4.jar bsh.Console
java -cp .;selenium-server-standalone-2.15.0.jar;bsh-2.0b4.jar bsh.Interpreter
select the option you like best starting up an IDE like GUI console shell (bsh.Console) or use existing command prompt shell of your OS (bsh.Interpreter).
From there, you just use normal Java code like:
WebDriver driver = new FirefoxDriver();
There may be some caveats from using interpreter shell as I’ve found for using WebDriver sendKeys() method, you have to write the command differently in the shell:
see reference post here on reasoning for that:
Once you got code you like, you can then copy & paste into your real Java code in IDE to complete it and run formal test.
Happy playing with Java interpreter shell now…particularly with WebDriver and Selenium!