Can compile Scala programs but can't run them

ε祈祈猫儿з 提交于 2019-12-03 12:13:44

问题


I am able to compile scala programs using scalac in terminal but I get the warning.

Charless-Macintosh:src Charles$ scalac hello.scala
Charless-Macintosh:src Charles$ scala HelloWorld
No such file or class on classpath: HelloWorld

Is this to do with .profile on scala. I'm pretty confused as to what is happening. Many thanks


回答1:


The current directory is typically not in the classpath by default. So you need to give explicitly:

$ scala -cp . HelloWorld



回答2:


The problem is that you have set the CLASSPATH environment variable.

From > man scalac:

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

When you have the CLASSPATH variable set, scala will not include the current directory in its search, you must explicitly add it. This is why scala -cp . HelloWorld works.

To verify, perform echo CLASSPATH and it should give some non-empty string. Check your .bashrc/.zshrc files for any export CLASSPATH=... and remove these lines.




回答3:


This was also happening to me but I think the better solution is to modify the CLASSPATH variable to include the current directory in addition to what you already had. e.g.

export CLASSPATH=.:$CLASSPATH

Now, you can simply use scala HelloWorld without that additional argument.



来源:https://stackoverflow.com/questions/27998824/can-compile-scala-programs-but-cant-run-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!