I'm using a compiled Java file which takes a filename as a parameter and then asks for a username and password (via user input). I am trying to automate this from a shell script, but am running into problems. I'm unable to access the java code, which is why I am having trouble.
So the code is called the following way
java javaprogram /home/user/securityfile
So, you pass the file, and then it asks you for username and password. After entering those, it's done.
Now, I have tried to put the user input into a file and pass it in, but I get an error.
java javaprogram /home/user/securityfile < userinputfile
userinput file contains the following text (First line is the username, second line is the password):
MattSmith
MSpassword136
Does anyone have any ideas? Maybe I am doing it wrong?
Here is the error message:
Exception in thread "main" java.lang.NullPointerException at ilex.util.UserAuthFile.main(UserAuthFile.java:297)
Thanks
Try
(echo "user"; sleep 1; echo "password") | java javaprogram /home/user/securityfile
If that works you can refactor to read from file without too much work. If it doesn't, may be that app will never read from System.in since it requires an interactive console System.console(). Some info here.
Related
im trying to mask password when i type my user input password it must be hidden or *. im using this readPassowrd method which has issues like null pointer error for performing the task in IDE but fine for Command line.
Is there anyways i can mask password in IDE only.
this is what i have below
char[] PASSWORD =console.readPassword("ENTER YOUR PASSWORD :");
log.info("Entered" + PASSWORD);
but i get Exception in thread "main" java.lang.NullPointerException in console in the IDE.
Any help with this ?
It is well known that java.io.Console is NOT actually available always, but it depends by the implementation of your runtime environment.
Normally, it's hard to find an IDE which provides an instance of Console class, that's why you get NullPointerException, simply because when you launch your application with the IDE's "Run" command, the System.console() method returns null.
I am afraid this is not an issue of Eclipse, but it happens with IntelliJ and many other IDEs as well.
Personally, if I need to use the Console, what I normally do is to launch the application via the Terminal, because the JVM launched via java command it always returns a not-null instance of java.io.Console, so you can also test the readPassword method.
You can launch the terminal in Eclipse by right-clicking on output folder of your application and then select the menu "Show In -> Terminal", then you can launch the classic "java" command (hope you know how it works):
> java -cp . <YourMainClass>
You can try this:
Console cnsl = null;
try {
// creates a console object
cnsl = System.console();
// if console is not null
if (cnsl != null) {
// read password into the char array
char[] pwd = cnsl.readPassword("Password: ");
// prints
System.out.println("Password is: "+pwd);
}
} catch(Exception ex) {
// if any error occurs
ex.printStackTrace();
}
As it is described here
Here is a piece of code to read user input using Scanner.
Scanner inner = new Scanner(System.in);
Logger logger = Logger.getLogger("Test");
logger.info("Before");
int a = inner.nextInt();
logger.info("After");
......
When I use ant to run a java task and excute my code(with "fork=true"), the program get stuck after printing "Before". I can input anything but the "After" never get printed.
However, when using command line java:
java -cp build/BoxBugRunner.jar:lib/gridworld.jar com.perqin.boxbugrunner.BoxBugRunner
the input is accepted and everything works fine.
It seems that System input cannot be access when using ant to run java program, so how to solve this problem?
Instead of using System.in you should:
Split your program in the part before and after the input. In the build script execute the first part, then get the input via the Ant input task, and finally execute the second part with the entered parameters.
Or write your your program as Ant task that delegates to the Ant input task for prompting for input.
So I'm creating a Java program and I want to make it so that you can ask it to open a program.
But, here's the catch, I want the program it opens to be taken from the user input, right now I'm trying to change this
try{Process p = Runtime.getRuntime().exec("notepad.exe");}
catch(Exception e1){}
Into something that opens a program that you asked it to open.
Here's an example of what I want:
User: Can you open chrome?
Program: Of course, here you go!
chrome opens
Could anyone tell me how I would be able to do this?
You can do it in two ways:
1.By Using Runtime:
Runtime.getRuntime().exec(...)
So, for example, on Windows,
Runtime.getRuntime().exec("C:\application.exe -arg1 -arg2");
2.By Using ProcessBuilder:
ProcessBuilder b = new ProcessBuilder("C:\application.exe", "-arg1", "-arg2");
or alternatively
List<String> params = java.util.Arrays.asList("C:\application.exe", "-arg1", "-arg2");
ProcessBuilder b = new ProcessBuilder(params);
or
ProcessBuilder b = new ProcessBuilder("C:\application.exe -arg1 -arg2");
The difference between the two is :
Runtime.getRuntime().exec(...)
takes a single string and passes it directly to a shell or cmd.exe process. The ProcessBuilder constructors, on the other hand, take a varargs array of strings or a List of strings, where each string in the array or list is assumed to be an individual argument.
So,Runtime.getRuntime.exec() will pass the line C:\application.exe -arg1 -arg2 to cmd.exe, which runs a application.exe program with the two given arguments. However, ProcessBuilder method will fail, unless there happens to be a program whose name is application.exe -arg1 -arg2 in C:.
You can try it with like. Pass whole path of where you install chrome.
try{
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
}
catch(Exception e1){
}
When using exec, it is essentially the same as if you were using the command line on windows. Open Command Prompt, type open, and see if it gives details as to how it opens files. If not, find the opener. Usually when dealing with command line operations, there are multiple parameters that are required for opening files/applications. An example of this would be for opening the "TextEdit.app" application on a mac.
Process p = Runtime.getRuntime().exec("open -a TextEdit.app");
Terminal(for mac) would open the app using the -a flag, meaning "application." You could open a file doing:
Process p = Runtime.getRuntime().exec("open filename.file_ext -a TextEdit.app");
The second one will tell the computer to find the application named <app_name>.app and open the file filename.file_ext
I know this is not going to work for a windows machine, but it's only to show how to use the command line operations for opening files and applications. It should be similar for windows though.
Hope this helps
I'm testing tidesdk.
I have a java program that reads from standard input.
I run the program through the console console
java -cp MyProgram.jar package.MyMainClass
And then execute commands and get results.
there any way to do with tidesdk?
Edit:
The problem was that calls the java program with a list of one element (which contained the command separated by spaces)
It solved with passing every word to a item of list (and removing the spaces).
Right now I have porblemas to write standard input. This is what I'm trying.
var input = Ti.Process.createPipe();
var process = Ti.Process.createProcess({
args:['java', '-cp', 'C:/.../MyProgram.jar', 'package.MyMainClass'],
stdin: input
});
//process.setOnReadLine(function(line) { alert(line) });
process.launch();
input.write("comand parameter1 parameter2\n"); //This line does not work
The java program starts. But never gets a command.
Checkout Documentation of Ti.Process.createProcess. That is exactly what you are looking for:
http://tidesdk.multipart.net/docs/user-dev/generated/#!/api/Ti.Process
I usually run this program via a command line like so:
java Program <TestClass.java
Which as I understand, forces the contents of TestClass.java to the console as user input.
i.e. It would be like executing
java Program
and then typing what ever is in TestClass.java
My problem is getting this happening in Eclipse. I can't figure out how to do it.
I would have thought that adding
<TestClass.java
to the program arguments in the run configuration would work, but it seems not.
Any suggestions?
How about adding this on top of your main.
InputStream in;
if (args.length > 0) {
in = new FileInputStream(args[0]);
} else {
// fallback
in = System.in;
}
And then you add the filename as an argument, as if you're running java Program TestClass.java. This way, it will work whether you run it as before or using the filename as an argument.