This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Running a compiled class

2 replies
Derek
Joined: 2011-10-12,
User offline. Last seen 42 years 45 weeks ago.

Either something drastic has changed since I last tried this - or I'm
suffering from even more extreme brain fade than normal ....

I have a small scala prog which I put together to play with the
httpclient libraries. It works (runs) in eclipse just fine. scalac
compiles it to a class without errors - but when I try and run
the .class file it fails with a java.lang.ClassNotFoundException. The
exception is pointing to the class that was just compiled and is in
the current directory. So I added that directory by name to the
classpath - same result. So I tried adding a java class to call it in
eclipse - and that fails to compile/run for what appears to be an
associated reason - 'Cannot figure out how to run target'

Code snippets are below, help and suggestions using short words please
- clearly I am currently incapable of advanced understanding today.

/************************
Scala object with main
*************************/
package sp1

object ReadNet {

def main(args: Array[String]) {

val s1 = "http"
val i1 = -1
var site = "graphical.weather.gov"
var s3 = "/xml/sample_products/browser_interface/
ndfdXMLclient.php"
var s4 = "lat=38.799697&lon=-121.271962&product=time-
series&startTime=2004-01-01T00:00:00&endTime=2013-04-20T00:00:00&maxt=maxt&mint=mint"

var uri: URI = URIUtils.createURI(s1, site, i1, s3,s4, null);

try {
var stuff = new GetStuff(uri)
val newForecast = new(CreateForecastObject)(stuff.xml)
} catch {
case ex : RuntimeException => println ("connect failed with " +
ex)
}

}

}
/********************
Java calling prog
********************/
package scala.loader;

import java.util.*;

public class ScalaEntryPoint {

public static void main(String[] args) {
List argList = new ArrayList();
argList.add("ReadNet");
for (String s : args) argList.add(s);
scala.tools.nsc.MainGenericRunner.main(argList.toArray(new
String[0]));

}

}

/*********************
class invocation error
********************/
PS C:\dev\workspace\sc1\src\sp1> scala -howtorun:object ReadNet.class
java.lang.ClassNotFoundException: ReadNet.class (args = , classpath =
C:\glassfish3\jdk\jre\lib\resources.jar;C:\glassfish3\jdk\jre\lib
\rt.jar;C:\glassfish3\jdk\jre\lib\jsse.jar;C:\glassfish3\jdk\jre\lib
\jce.jar;C:\glassfish3\jdk\jre\lib\ch arsets.jar;C:\glassfish3\jdk\jre
\lib\ext\dnsns.jar;C:\glassfish3\jdk\jre\lib\ext\localedata.jar;C:
\glassfish3\jdk\jre\li b\ext\sunjce_provider.jar;C:\dev\SCALA-~1.FIN
\bin\..\lib\jline.jar;C:\dev\SCALA-~1.FIN\bin\..\lib\scala-
compiler.jar;C:\ dev\SCALA-~1.FIN\bin\..\lib\scala-dbc.jar;C:\dev
\SCALA-~1.FIN\bin\..\lib\scala-library.jar;C:\dev\SCALA-~1.FIN\bin\..
\li b\scala-swing.jar;C:\dev\SCALA-~1.FIN\bin\..\lib\scalap.jar;C:
\glassfish3\jdk\lib;C:\dev\workspace\sc1\src\sp1)

Derek
Joined: 2011-10-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Running a compiled class

Thanks to Som Snytt for pointing out the nesting issue - scala -cp .
sp1.ReadNet causes the class to be found.

So now - that (inevitably) turns up another issue.

When it tries to create a uri I get java.lang.NoClassDefFoundError:
org/apache/http/client/utils/URIUtils. Eclipse runs the scala prog &
it works. When I invoke the java entry point - I get that error. But
they have the same build path and classpath. When I run it from the
command line (as above) I get the error. The httpclient jar is
explicitly called out in the Environment classpath.

Derek
Joined: 2011-10-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Running a compiled class

I've had a number of hints (thanks guys) about putting the classpath
in the invocation. That works fine for the command line. Now what I'm
having problems with is getting the java entry point program to supply
that information so that it can be utilised in GenericRunnerCommand.
As far as I can tell the classpath referenced in this line .....

else if (util.ScalaClassLoader.classExists(settings.classpathURLs,
target)) AsObject

is null. So - any hints on how to populate that?

Thanks
Derek

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland