2

The problem I have is larger, but I will simplify the concept that is failing.

I am working on Ubuntu.

Here is my directory structure:

~/mydirectory

--/groovy

--/myjavafiles

I have a script, script.groovy, that lives inside ~/mydirectory/groovy and a java file called Hello.java that lives inside ~/mydirectory/myjavafiles. script.groovy has the following inside:

#!/usr/bin/env groovy

package groovy;
import myjavafiles.Hello;

println("hello");

Hello.java has this:

package myjavafiles;
public class Hello {
   public Hello() {
       System.out.println("hello");
   }
}

I have tried running:

$./script.groovy

aswell as

$groovy script.groovy

But I only get an error, "unable to find class".

Here are the steps I have taken to fix this error:

i. set CLASSPATH = ~/mydirectory, that didn't work.

ii. used

$jar cf myjavafiles.jar myjavafiles

and placed myjavafiles.jar in ~/.groovy/lib, that didn't work.

iii. As mentioned here, I tried to modify groovy.script as follows:

#!/bin/bash
//usr/bin/env groovy
package groovy;
import myjavafiles.Hello;

println("hello");

That also did not work.

Other, maybe relevant

If it's any help, I'm using Ubuntu, Java 7 and Groovy 2.1.5

GROOVY_HOME=/opt/groovy/groovy-2.1.5/

and $GROOVY_HOME/bin is in my PATH

I would greatly appreciate any help.

Community
  • 1
  • 1
neastin
  • 23
  • 1
  • 4
  • [As Tim mentioned](http://stackoverflow.com/a/17300114/459743) you're declaring your java class in one package, but trying to import it from another. – codelark Jun 25 '13 at 14:36

1 Answers1

1

Change script.groovy to:

package groovy
import myjavafiles.Hello

println "hello"

Compile the java code with:

javac myjavafiles/Hello.java

Then run

groovy groovy/script.groovy
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I somehow made a mistake and didn't include the right file. The import I am using and failed to use was myjavafiles.Hello I will try to compile the java code. Is that something I would have to do every single time? Is there any way of compiling a whole directory of java files?. – neastin Jun 25 '13 at 14:43
  • You will need to compile it if you change it. Have you tried `javac myjavafiles/*.java`? Also, you might look in to using something like [gradle](http://www.gradle.org/) to do the building for you – tim_yates Jun 25 '13 at 14:46
  • I compiled the Hello.java, which produced a Hello.class file. However, when I tried groovy script.groovy, it failed. It seems to only work when I am in ~/mydirectory, and do groovy groovy/script.groovy. In summary, it works.. but why does it have to be ran that way? – neastin Jun 25 '13 at 14:52
  • the `Hello.class` file is in `myjavafiles` yeah? Then you ran `groovy groovy/script.groovy`? – tim_yates Jun 25 '13 at 14:53
  • Re-read my comment above. I edited it right after I submitted. I think I got it working, but only if done in a very specific way. – neastin Jun 25 '13 at 15:11
  • @neastin it will only work that way as you have stuck things in packages so it has to be that way... You can run it (assuming the groovy file is in `groovy` and the java class file is in `myjavafiles`) from anywhere else using `groovy -cp ~/mydirectory ~/mydirectory/groovy/script.sh` – tim_yates Jun 25 '13 at 15:15