0

I am trying to run my java program through bash in a linux system where the command is:

java -Dresources=path1 -Dinstance=instance1 -cp ./lib/jar1.jar;./lib/folder1/*;./lib/common/*; lib/folder2/* com.test.host

In the host class I am reading some XML files, which has spring beans and I am loading them. This command works perfectly fine in windows. However, when I run this in Bash shell. I get the following exception:

syntax error near unexpected token newline 'xml version= 1.0 encoding= utf-8'

I tried removing unnecessary spaces in the XML and tried but to no avail.

  • 2
    On Linux, you need to separate elements of the classpath with `:` (colon), not `;` (semicolon). Though I don't see how that difference would lead to this error, I would recommend you try it. If that doesn't solve it, please provide a [mre]. – Mark Rotteveel Jun 25 '22 at 07:16
  • The error itself - at a guess - suggests you have an XML file that starts with `\nxml version = 1.0 encoding = utf-8` (that is, with a newline after ``) instead of ` – Mark Rotteveel Jun 25 '22 at 07:18
  • I also notice a space before the last element of the classpath (before `lib/folder2/*`). This could also be the problem. Remove that space. – Mark Rotteveel Jun 25 '22 at 07:21
  • 3
    This is a [bash error](https://stackoverflow.com/questions/5134399/bash-syntax-error-near-unexpected-token-newline). Java doesn't even start. – Olivier Jun 25 '22 at 07:21
  • @Olivier Would the `;` cause bash to execute the following as a statement? – Mark Rotteveel Jun 25 '22 at 07:22
  • @MarkRotteveel Yes, I think so. – Olivier Jun 25 '22 at 07:23
  • The command before the `;` (i.e `java`) should have been executed first and should have printed and error and usage. – Diego Torres Milano Jun 25 '22 at 07:28
  • You won't get that error with *that* code. The first thing you will see is a Java usage error message since the Java command (which ends with the first `;`) does not receive a class name as an argument (nor a `-jar` arg of course) as @DiegoTorresMilano has said – g00se Jun 25 '22 at 11:12
  • @MarkRotteveel - you are right, after replacing ; (semi-colon) with the : (colon) , the command works as intended. Thanks for the info. However, I am not sure why did java not detect the error with the usage and threw an error/exception. – Sumit Kumar Mahto Jun 27 '22 at 09:49

1 Answers1

0

As @MarkRotteveel mentioned, after replacing ; (semi-colon) with the : (colon) , the command works as intended. However, I am not sure why did java not detect the error with the usage and threw an error/exception