简体   繁体   English

用CLASSPATH环境变量替换classpath参数

[英]Replacing classpath argument with CLASSPATH environment variable

Requirements: 要求:

  • Command-line only, ie no IDE's 仅命令行,即没有IDE
  • Do not want to use classpath argument 不想使用classpath参数
  • OS: OS X v10.6.8 作业系统:OS X v10.6.8

Steps: 脚步:

  • Setup CLASSPATH environment variable: export CLASSPATH="/path-jar-files-are-at/*" 设置CLASSPATH环境变量: export CLASSPATH="/path-jar-files-are-at/*"
  • Compile my java app: javac MyApp.java 编译我的Java应用程序: javac MyApp.java
  • Launch my java app: java MyApp 启动我的Java应用程序: java MyApp

After launching, the following error appears: 启动后,出现以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: MyApp
Caused by: java.lang.ClassNotFoundException: MyApp

Using classpath argument doesn't have this problem 使用classpath参数没有这个问题

  • Compile my java app: javac -classpath /path-jar-files-are-at/* MyApp.java 编译我的Java应用程序: javac -classpath /path-jar-files-are-at/* MyApp.java
  • Launch my java app: java -classpath /path-jar-files-are-at/* MyApp 启动我的Java应用程序: java -classpath /path-jar-files-are-at/* MyApp

Any ideas? 有任何想法吗?

The CLASSPATH should not contain the trailing "/*" - that is not going to give you a valid classpath. CLASSPATH不应包含结尾的“ / *”-不会为您提供有效的类路径。 The shell is expanding the /* and separating the list with whitespace/newlines - rather than the ":" character. Shell正在扩展/ *并使用空格/换行符分隔列表-而不是“:”字符。

If you still really want it to work this (dynamic) way, try something like this: 如果您仍然真的希望它以这种(动态)方式工作,请尝试如下操作:

export CP=`ls /path-jar-files-are-at/*`; CP=`echo $CP | sed 's/\.jar\s/.jar:/g'`; CP="$CP:/path-jar-files-are-at"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM