0

I am not able to compile and run Groovy-scripts. I take a very first example Gold.groovy . I can compile it with groovyc Gold.groovy

when I try to start it thereafter with: java Gold

I get the error message

"Fehler: Hauptklasse Gold konnte nicht gefunden oder geladen werden" - which means in english ~ "Main class not found"

Has anybody a hint what I could try or where to find the error?

Thanks in forehand

source

List fibo = [1, 1]                          //#A
List gold = [1, 2]                          //#B

while ( ! isGolden( gold[-1] ) ) {          //#C
    fibo.add( fibo[-1] + fibo[-2] )         //#D
    gold.add( fibo[-1] / fibo[-2] )         //#E
}

println "found golden ratio with fibo(${ fibo.size-1 }) as"
println fibo[-1] + " / " + fibo[-2] + " = " + gold[-1]
println "_" * 10 +  "|"  + "_" * (10 * gold[-1])

def isGolden(candidate) {                   //#F
    def small = 1                           //#G
    def big = small * candidate             //#H
    return isCloseEnough( (small+big)/big, big/small)
}

def isCloseEnough(a,b) { return (a-b).abs() < 1.0e-9 }
//#A Initial Fibonacci numbers
//#B Golden ration candidates
//#C Last gold candidate
//#D Next fibo number
//#E Next golden candidate
//#F Candidate satisfies golden rule
//#G Smaller section
//#H Bigger section

for request: screen output

D:\work\groovy\GroovyInAction-master\listings\chap01>set CLASSPATH
CLASSPATH=.;D:\DevTools\groovy-2.4.7\embeddable\groovy-all-2.4.7.jar

D:\work\groovy\GroovyInAction-master\listings\chap01>type HelloWorld.groovy
println "Hello World"

D:\work\groovy\GroovyInAction-master\listings\chap01>groovyc HelloWorld.groovy

D:\work\groovy\GroovyInAction-master\listings\chap01>java WelloWorld
Fehler: Hauptklasse WelloWorld konnte nicht gefunden oder geladen werden

D:\work\groovy\GroovyInAction-master\listings\chap01>javac -cp .;D:\DevTools\gro
ovy-2.4.7\embeddable\groovy-all-2.4.7.jar HelloWorld
error: Class names, 'HelloWorld', are only accepted if annotation processing is
explicitly requested
1 error
ucMedia
  • 4,105
  • 4
  • 38
  • 46
toohoo
  • 3
  • 6

2 Answers2

1

Gold.groovy:

def name = 'world'
println "hello $name!"

compile:

groovyc Gold.groovy

produces Gold.class

run (for unix use : instead of ; in the classpath):

java -cp embeddable\groovy-all-2.4.11.jar;. Gold

output:

hello world!
daggett
  • 26,404
  • 3
  • 40
  • 56
  • Thanks, all the same here but Output: "Fehler: Hauptklasse Gold konnte nicht gefunden oder geladen werden" ~ [EN] Main class not found – toohoo Jun 08 '17 at 08:32
  • look, if i specify non-existing class name in the classpath and german localization: `java -cp embeddable\groovy-all-2.4.11.jar;. -Duser.language=de Gold1` i got the same message as you: `Fehler: Hauptklasse Gold1 konnte nicht gefunden oder geladen werden`. that means that your class Named `Gold` is not in the classpath defined by `-cp` parameter. Could you show your command line? – daggett Jun 08 '17 at 09:17
  • D:\work\groovy\GroovyInAction-master\listings\chap01>type HelloWorld.groovy println "Hello World" D:\work\groovy\GroovyInAction-master\listings\chap01>groovyc HelloWorld.groovy D:\work\groovy\GroovyInAction-master\listings\chap01>set CLASSPATH CLASSPATH=.;D:\DevTools\groovy-2.4.7\embeddable\groovy-all-2.3.2.jar D:\work\groovy\GroovyInAction-master\listings\chap01>java -cp .;D:\DevTools\groo vy-2.4.7\embeddable\groovy-all-2.3.2.jar HelloWorld Fehler: Hauptklasse HelloWorld konnte nicht gefunden oder geladen werden – toohoo Jun 08 '17 at 11:01
  • D:\work\groovy\GroovyInAction-master\listings\chap01>java -cp .;D:\DevTools\groo vy-2.4.7\embeddable\groovy-all-2.3.2.jar -Duser.language=en HelloWorld Error: Could not find or load main class HelloWorld – toohoo Jun 08 '17 at 11:04
  • another guess: maybe you have several java? check `set JAVA_HOME` and `java -version`. It should work. Problem somewhere in your environment... – daggett Jun 08 '17 at 11:28
  • maybe you have package definition in your script? – daggett Jun 08 '17 at 11:32
  • I did a screen listing of my mini-script above. It consists of one line code: ´´println "Hello World"´´ - there aren't any further definitions in it. – toohoo Jun 08 '17 at 11:55
  • Hello @toohoo, if you are still stuck, could you paste that screen listing in the question? It will be easier for us to help. Apparently you reproduce the problem with a very simple script (1 line), the issue is in the environment or the steps, so we need to see the exact steps, a bit more clearly than in a comment. Thanks in advance ;) – Hugues M. Jun 08 '17 at 15:45
  • i see the strange thing in your classpath: `D:\DevTools\groovy-2.4.7\embeddable\groovy-all-2.3.2.jar` !!! `groovy-2.4.7` and `groovy-all-2.3.2`. That's two different versions of groovy. Quite sure that's non-existing path! – daggett Jun 08 '17 at 15:52
  • Thanks to @daggett , this was false but did not solve the problem. (or in other words, I have now another error) – toohoo Jun 09 '17 at 07:20
  • Thanks to @Huges Moreau , I pastet the screen output in the question – toohoo Jun 09 '17 at 07:20
  • @toohoo, in you last command there is `javac` instead of `java`. Could you provide the result of the following commands: `groovy -version` `java -version` `set JAVA_HOME` `set PATH` – daggett Jun 09 '17 at 07:30
  • @dagget sorry you are right, possibly a typo. now all is working. Thanks a lot to all! Have A Nice Day – toohoo Jun 09 '17 at 08:59
0

You should use groovy Gold.groovy

Java actually does not know about groovy scripts. A script is always compiled into a class. The Groovy compiler will compile the class for you, with the body of the script copied into a run() method.

Uladzislau Kaminski
  • 2,113
  • 2
  • 14
  • 33
  • Thanks for the hints. I know that groovyc is compiling the script to a class. I have the Gold.class in the directory. So if Gold.class is available I thought the command "java Gold" should start the class. I did read in the book, that groovyc would automatically enclose the script with a main method. Am I right? – toohoo Jun 08 '17 at 07:51
  • @toohoo I believe java is looking for main class, but groovyc is complie this code into `run()` method. So, you can't run `java Gold` – Uladzislau Kaminski Jun 08 '17 at 08:45