@@ -230,17 +230,15 @@ public void runImpl(CommandLine cli) throws Exception {
230230 throw new IllegalArgumentException (
231231 "Value of --script option is invalid! " + script + " not found" );
232232 } else {
233- Path scriptFile = serverDir .getParent ().resolve ("bin" ).resolve ("solr" );
233+ Path scriptFile =
234+ serverDir .getParent ().resolve ("bin" ).resolve (CLIUtils .isWindows () ? "solr.cmd" : "solr" );
234235 if (Files .isRegularFile (scriptFile )) {
235236 script = scriptFile .toAbsolutePath ().toString ();
236237 } else {
237- scriptFile = serverDir .getParent ().resolve ("bin" ).resolve ("solr.cmd" );
238- if (Files .isRegularFile (scriptFile )) {
239- script = scriptFile .toAbsolutePath ().toString ();
240- } else {
241- throw new IllegalArgumentException (
242- "Cannot locate the bin/solr script! Please pass --script to this application." );
243- }
238+ throw new IllegalArgumentException (
239+ "Cannot locate the bin/"
240+ + scriptFile .getFileName ().toString ()
241+ + " script! Please pass --script to this application." );
244242 }
245243 }
246244
@@ -688,7 +686,8 @@ protected Map<String, Object> startSolr(
688686 String verboseArg = isVerbose () ? "--verbose" : "" ;
689687
690688 String jvmOpts = cli .getOptionValue (JVM_OPTS_OPTION );
691- String jvmOptsArg = (jvmOpts != null ) ? " --jvm-opts \" " + jvmOpts + "\" " : "" ;
689+ String jvmOptsArg =
690+ (jvmOpts != null && !jvmOpts .isEmpty ()) ? " --jvm-opts \" " + jvmOpts + "\" " : "" ;
692691
693692 Path cwd = Path .of (System .getProperty ("user.dir" ));
694693 Path binDir = Path .of (script ).getParent ();
@@ -708,10 +707,10 @@ protected Map<String, Object> startSolr(
708707 ? "-Dsolr.modules=clustering,extraction,langid,ltr,scripting -Dsolr.ltr.enabled=true -Dsolr.clustering.enabled=true"
709708 : "" ;
710709
711- String startCmd =
710+ String startCmdStr =
712711 String .format (
713712 Locale .ROOT ,
714- "\" %s\" start %s -p %d --solr-home \" %s\" --server-dir \" %s\" %s %s %s %s %s %s %s %s " ,
713+ "\" %s\" start %s -p %d --solr-home \" %s\" --server-dir \" %s\" %s %s %s %s %s %s %s" ,
715714 callScript ,
716715 cloudModeArg ,
717716 port ,
@@ -723,12 +722,11 @@ protected Map<String, Object> startSolr(
723722 forceArg ,
724723 verboseArg ,
725724 extraArgs ,
726- jvmOptsArg ,
727725 syspropArg );
728- startCmd = startCmd .replaceAll ("\\ s+" , " " ).trim (); // for pretty printing
726+ startCmdStr = startCmdStr .replaceAll ("\\ s+" , " " ).trim (); // for pretty printing
729727
730728 echo ("\n Starting up Solr on port " + port + " using command:" );
731- echo (startCmd + "\n " );
729+ echo (startCmdStr + jvmOptsArg + "\n " );
732730
733731 String solrUrl =
734732 String .format (
@@ -757,8 +755,21 @@ protected Map<String, Object> startSolr(
757755 }
758756 }
759757 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler ();
760- executor .execute (org .apache .commons .exec .CommandLine .parse (startCmd ), startEnv , handler );
761-
758+ org .apache .commons .exec .CommandLine startCmd =
759+ org .apache .commons .exec .CommandLine .parse (startCmdStr );
760+
761+ if (!jvmOptsArg .isEmpty ()) {
762+ startCmd .addArgument ("--jvm-opts" );
763+
764+ /* CommandLine.parse() tends to strip off the quotes by default before sending to cmd.exe.
765+ This may cause cmd to break up the argument value on certain characters in unintended ways
766+ thereby passing incorrect value to start.cmd
767+ (eg: for "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:18983", it
768+ breaks apart the value at "-agentlib:jdwp" passing incorrect args to start.cmd ).
769+ The 'false' here tells Exec: “don’t touch my quotes—this is one atomic argument” */
770+ startCmd .addArgument ("\" " + jvmOpts + "\" " , false );
771+ }
772+ executor .execute (startCmd , startEnv , handler );
762773 // wait for execution.
763774 try {
764775 handler .waitFor (3000 );
@@ -767,21 +778,25 @@ protected Map<String, Object> startSolr(
767778 Thread .interrupted ();
768779 }
769780 if (handler .hasResult () && handler .getExitValue () != 0 ) {
781+ startCmdStr += jvmOptsArg ;
770782 throw new Exception (
771783 "Failed to start Solr using command: "
772- + startCmd
784+ + startCmdStr
773785 + " Exception : "
774786 + handler .getException ());
775787 }
776788 } else {
789+ // Unlike Windows, special handling of jvmOpts is not required on linux. We can simply
790+ // concatenate to form the complete command
791+ startCmdStr += jvmOptsArg ;
777792 try {
778- code = executor .execute (org .apache .commons .exec .CommandLine .parse (startCmd ));
793+ code = executor .execute (org .apache .commons .exec .CommandLine .parse (startCmdStr ));
779794 } catch (ExecuteException e ) {
780795 throw new Exception (
781- "Failed to start Solr using command: " + startCmd + " Exception : " + e );
796+ "Failed to start Solr using command: " + startCmdStr + " Exception : " + e );
782797 }
798+ if (code != 0 ) throw new Exception ("Failed to start Solr using command: " + startCmdStr );
783799 }
784- if (code != 0 ) throw new Exception ("Failed to start Solr using command: " + startCmd );
785800
786801 return getNodeStatus (
787802 solrUrl , cli .getOptionValue (CommonCLIOptions .CREDENTIALS_OPTION ), maxWaitSecs );
0 commit comments