I'm having a bit of trouble with a java assignment involving inputs and javac. I need to be able to call various methods in my program from the terminal, but its only running the main, for example:

javac -d . test.java
java test/Test -testMethod (1+2)

would return whatever happens in the main when it should only run testMethod

My program methods work in netbeans, but it needs to be able to run from the terminal with whats above (testMethod would print the solution of the expression given to it via string).

有帮助吗?

解决方案

Executing a Java program will only execute the main method.

If you need user interactive functionality in your program, then this needs to be accounted for in your main method. Consider looking into the Scanner class which allows users to interact with programs by providing inputs.

You can also pass a String[] array called args to the main method which you can parse, but this interactivity only happens once at the start of runtime

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top