我查找了solr是如何找到solrhome然后加载配置文件的。
/**
327 * Determines the instanceDir from the environment.
328 * Tries JNDI (java:comp/env/solr/home) then system property (solr.solr.home);
329 * if both fail, defaults to solr/
330 * @return the instance directory name
331 */
332 /**
333 * Finds the instanceDir based on looking up the value in one of three places:
334 *
335 * JNDI: via java:comp/env/solr/home
336 * The system property solr.solr.home
337 * Look in the current working directory for a solr/ directory
338 *
339 *
340 * The return value is normalized. Normalization essentially means it ends in a trailing slash.
341 * @return A normalized instanceDir
342 *
343 * @see #normalizeDir(String)
344 */
345 public static String locateInstanceDir() {
346 String home = null;
347 // Try JNDI
348 try {
349 Context c = new InitialContext();
350 home = (String)c.lookup("java:comp/env/"+project+"/home");
351 log.info("Using JNDI solr.home: "+home );
352 } catch (NoInitialContextException e) {
353 log.info("JNDI not configured for "+project+" (NoInitialContextEx)");
354 } catch (NamingException e) {
355 log.info("No /"+project+"/home in JNDI");
356 } catch( RuntimeException ex ) {
357 log.warning("Odd RuntimeException while testing for JNDI: " + ex.getMessage());
358 }
359
360 // Now try system property
361 if( home == null ) {
362 String prop = project + ".solr.home";
363 home = System.getProperty(prop);
364 if( home != null ) {
365 log.info("using system property "+prop+": " + home );
366 }
367 }
368
369 // if all else fails, try
370 if( home == null ) {
371 home = project + '/';
372 log.info(project + " home defaulted to '" + home + "' (could not find system property or JNDI)");
373 }
374 return normalizeDir( home );
375 }
互联网上说的通过环境变量的方式是在第363行,其中的project值为solr.所以环境变量的应当为solr.solr.home.
可是System.getProperty是获取系统属性,不是获到环境变量.获取环境变量的方式应当是:System.getenv
至少在我的机器上面是这样,如果你的机器上面也取不到.那么你就应当和我是一样的.原文中的注释也是这么说的。