4

I have a noob question, but i can't figure it out.

I want to send from command line a string with multiple lines separated by '\n'

public class Test {

 public static void main(String args[]){
    System.out.println(args[0]);
 }

}

And I run with

java -jar test.jar 'te\nst'

I expect result

te 
st

but I'm getting

tenst

I'm using Java 7 and running in Ubuntu.

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • Use `-Dmessage="$(printf 'te\nst\n')"` – fge Dec 22 '14 at 16:16
  • Isn't there a better way you can achieve this? Why does it have to be a VM argument? – Sotirios Delimanolis Dec 22 '14 at 16:21
  • Since `main` takes in all the arguments, can't you get that value, substring from the equals to the end, use `StringTokenizer` on the \ and print them out that way? – Ascalonian Dec 22 '14 at 16:23
  • @SotiriosDelimanolis without vm argument and using args doesn't work neither – nachokk Dec 22 '14 at 16:24
  • @Ascalonian yes i could but perhaps something more elegant already exist – nachokk Dec 22 '14 at 16:25
  • did you try `-Dmessage='te\nst'` and `-Dmessage="te\nst"` and `-Dmessage='te\\nst'`, etc? Either `System.out.println` doesn't work as you expect OR you just need to quote you message correctly so that the shell passes the correct string into `println`. Good luck. – shellter Dec 22 '14 at 16:36
  • you said you're using Ubuntu, maybe newline character have different coding in this operating system – niceman Dec 22 '14 at 16:40

4 Answers4

1

I wouldn't normally pass this sort of thing as a VM argument. I would perhaps pass it in a file (as noted elsewhere), or via stdin. If you do this via a heredoc then you don't need an intermediary file eg.

$ java MyProg <<EOF
te
st
EOF
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

I understand this may be undesirable for a number of reasons, but is it out of the question to store your multiple line message in a file and provide a URI directing your program to that file? This avoids using escaped characters, potentially difficult to interpret code, and a few other issues. A possible implementation may look like this:

message.txt:

te
st
//other lines

command:

java -jar -Dmessage=/path/to/message.txt

java:

public class Test {

 public static void main(String args[]){
    File msgFile = new File(System.getProperty("message"));
    BufferedReader msgBR = new BufferedReader(new FileReader(msgFile));
    String msg ="";
    String line = null;
    while ((line = msgBR.readLine()) != null) {
        msg += line + "\n";
    }
    System.out.println(msg);
 }

}

This could probably be cleaned up a bit, but it should get you to a possible solution. Leave a comment if you have any questions.

Matt
  • 5,404
  • 3
  • 27
  • 39
0

The problem is that im receiving '\\n' instead of '\n' so then i have different solutions One is make a script using echo -e

#!/bin/bash
VARIABLE=`echo -e "\n\nsomething\n\nhappens"`
java -jar test.jar "$VARIABLE"

another solution

  text = text.replace("\\n","\n");
nachokk
  • 14,363
  • 4
  • 24
  • 53
0

You labeled this as a noob question, but even though it is an easy question to ask, it is not an easy thing to answer in java. Java limits this type of evaluation to compile time, so either you need to write your own tool, or use another language. Fortunately, java includes other languages as shown in this question Is there an eval() function in Java?

The accepted answer uses the Javascript engine to evaluate strings. Here's an example combining that answer with your question.

import javax.script.*;

public class Test{
    public static void main (String args[]){
            String message = System.getProperty("message");
            System.out.println(message);
            try {
                    ScriptEngineManager factory = new ScriptEngineManager();
                    ScriptEngine engine = factory.getEngineByName("JavaScript");
                    engine.eval("print('" + message  + "')");
            } catch (Exception e)
            { System.out.println("Exception: " + e.getMessage()); }
    }
}

This program gives the following result:

C:\test>java -Dmessage=test Test
test
test
C:\test>java -Dmessage=te\nst Test
te\nst
te
st
Community
  • 1
  • 1
GregA100k
  • 1,385
  • 1
  • 11
  • 16