How to run a groovy file which include other groovy files please?










0















I have three groovy file: /my.groovy, /my_dir/util.groovy, /my_dir/base.groovy



my.groovy:



def shell = new GroovyShell()
def util = shell.parse(new File("my_dir/util.groovy"))
println(util.run());


util.groovy:



def getName(String name) 
def base = new base();
return name * base.getTimes();


println(getName('hi,'));


base.groovy:



def getTimes() 
return 20;



Now I run groovy my.groovy, and it can not work because unable to resolve class base. If these files all in the same dir, it can work. How to do it in this case please? (with no compile)










share|improve this question


























    0















    I have three groovy file: /my.groovy, /my_dir/util.groovy, /my_dir/base.groovy



    my.groovy:



    def shell = new GroovyShell()
    def util = shell.parse(new File("my_dir/util.groovy"))
    println(util.run());


    util.groovy:



    def getName(String name) 
    def base = new base();
    return name * base.getTimes();


    println(getName('hi,'));


    base.groovy:



    def getTimes() 
    return 20;



    Now I run groovy my.groovy, and it can not work because unable to resolve class base. If these files all in the same dir, it can work. How to do it in this case please? (with no compile)










    share|improve this question
























      0












      0








      0








      I have three groovy file: /my.groovy, /my_dir/util.groovy, /my_dir/base.groovy



      my.groovy:



      def shell = new GroovyShell()
      def util = shell.parse(new File("my_dir/util.groovy"))
      println(util.run());


      util.groovy:



      def getName(String name) 
      def base = new base();
      return name * base.getTimes();


      println(getName('hi,'));


      base.groovy:



      def getTimes() 
      return 20;



      Now I run groovy my.groovy, and it can not work because unable to resolve class base. If these files all in the same dir, it can work. How to do it in this case please? (with no compile)










      share|improve this question














      I have three groovy file: /my.groovy, /my_dir/util.groovy, /my_dir/base.groovy



      my.groovy:



      def shell = new GroovyShell()
      def util = shell.parse(new File("my_dir/util.groovy"))
      println(util.run());


      util.groovy:



      def getName(String name) 
      def base = new base();
      return name * base.getTimes();


      println(getName('hi,'));


      base.groovy:



      def getTimes() 
      return 20;



      Now I run groovy my.groovy, and it can not work because unable to resolve class base. If these files all in the same dir, it can work. How to do it in this case please? (with no compile)







      groovy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 12:18









      xunitcxunitc

      437




      437






















          1 Answer
          1






          active

          oldest

          votes


















          0














          A.groovy



          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          def greet(n)
          return "hello $n"



          assume you are in the directory where A.groovy located



          how to run:



          groovy -cp ./mydir/ A.groovy


          in this case you are running class A and specifying to lookup other classes from directory ./mydir/




          or you can use packages:



          A.groovy



          import mydir.*

          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          package mydir

          def greet(n)
          return "hello $n"



          how to run:



          groovy -cp . A.groovy





          share|improve this answer

























          • Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

            – xunitc
            Nov 15 '18 at 13:14











          • updated answer...

            – daggett
            Nov 15 '18 at 13:29










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319366%2fhow-to-run-a-groovy-file-which-include-other-groovy-files-please%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          A.groovy



          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          def greet(n)
          return "hello $n"



          assume you are in the directory where A.groovy located



          how to run:



          groovy -cp ./mydir/ A.groovy


          in this case you are running class A and specifying to lookup other classes from directory ./mydir/




          or you can use packages:



          A.groovy



          import mydir.*

          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          package mydir

          def greet(n)
          return "hello $n"



          how to run:



          groovy -cp . A.groovy





          share|improve this answer

























          • Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

            – xunitc
            Nov 15 '18 at 13:14











          • updated answer...

            – daggett
            Nov 15 '18 at 13:29















          0














          A.groovy



          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          def greet(n)
          return "hello $n"



          assume you are in the directory where A.groovy located



          how to run:



          groovy -cp ./mydir/ A.groovy


          in this case you are running class A and specifying to lookup other classes from directory ./mydir/




          or you can use packages:



          A.groovy



          import mydir.*

          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          package mydir

          def greet(n)
          return "hello $n"



          how to run:



          groovy -cp . A.groovy





          share|improve this answer

























          • Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

            – xunitc
            Nov 15 '18 at 13:14











          • updated answer...

            – daggett
            Nov 15 '18 at 13:29













          0












          0








          0







          A.groovy



          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          def greet(n)
          return "hello $n"



          assume you are in the directory where A.groovy located



          how to run:



          groovy -cp ./mydir/ A.groovy


          in this case you are running class A and specifying to lookup other classes from directory ./mydir/




          or you can use packages:



          A.groovy



          import mydir.*

          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          package mydir

          def greet(n)
          return "hello $n"



          how to run:



          groovy -cp . A.groovy





          share|improve this answer















          A.groovy



          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          def greet(n)
          return "hello $n"



          assume you are in the directory where A.groovy located



          how to run:



          groovy -cp ./mydir/ A.groovy


          in this case you are running class A and specifying to lookup other classes from directory ./mydir/




          or you can use packages:



          A.groovy



          import mydir.*

          def b=new B()
          println b.greet("world")


          ./mydir/B.groovy



          package mydir

          def greet(n)
          return "hello $n"



          how to run:



          groovy -cp . A.groovy






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 13:29

























          answered Nov 15 '18 at 13:07









          daggettdaggett

          8,93521530




          8,93521530












          • Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

            – xunitc
            Nov 15 '18 at 13:14











          • updated answer...

            – daggett
            Nov 15 '18 at 13:29

















          • Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

            – xunitc
            Nov 15 '18 at 13:14











          • updated answer...

            – daggett
            Nov 15 '18 at 13:29
















          Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

          – xunitc
          Nov 15 '18 at 13:14





          Thank you. I know it, I can use getClass() and get path in util.groovy to find the base.groovy. but I do not think it is an elegant solution. I means like python, just import file and can use the methods in that file. Could groovy do it?

          – xunitc
          Nov 15 '18 at 13:14













          updated answer...

          – daggett
          Nov 15 '18 at 13:29





          updated answer...

          – daggett
          Nov 15 '18 at 13:29



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319366%2fhow-to-run-a-groovy-file-which-include-other-groovy-files-please%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3