DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Full-Stack Observability Essentials: Explore the fundamentals of system-wide observability and key components of the OpenTelemetry standard.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Unlock the Power of Software Heritage Archive
  • The Easiest and Quickest Way to Generate an OpenAPI Spec for an Existing Website
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • How To Create a Stub in 5 Minutes

Trending

  • Exploring Different Continuous Integration Servers: Streamlining Software Development
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Protect Your Keys: Lessons from the Azure Key Breach
  • Agile Frameworks in Action: Enhancing Flexibility in Service Delivery
  1. DZone
  2. Coding
  3. Languages
  4. Groovy Goodness: Running Scripts from a JAR Archive

Groovy Goodness: Running Scripts from a JAR Archive

Dustin Marx user avatar by
Dustin Marx
·
Sep. 15, 13 · Interview
Like (0)
Save
Tweet
Share
8.29K Views

Join the DZone community and get the full member experience.

Join For Free

We can run Groovy scripts from the command-line with the groovy command followed by the name of Groovy script file. We could already run script with the http: protocol as described in this blog post. Since Groovy 2.1 we can even run scripts that are packaged in an archive with thejar: protocol.

Let's create first a simple Groovy script and package it in a sample.jar file.

// File: cookies.groovy

println 'Running from inside a JAR: '
def cookies = ['Cookie', 'Biscuit'].collect { it.toUpperCase() }.join(',')
println cookies

We can use the jar command to place the file cookies.groovy in sample.jar:

$ jar cvf sample.jar cookies.groovy
added manifest
adding: cookies.groovy(in = 153) (out= 126)(deflated 17%)
$ jar tvf sample.jar
0 Fri Sep 06 14:50:24 CEST 2013 META-INF/
68 Fri Sep 06 14:50:24 CEST 2013 META-INF/MANIFEST.MF
153 Fri Sep 06 14:48:44 CEST 2013 cookies.groovy
$

We now have a single Groovy script, but we can place more scripts inside the JAR file if we want to. To run a specific Groovy script file we must use the jar: protocol. We must specify the location of the JAR file with the file: or http: protocols followed by !/. In some shells the ! symbol is a special character and we might need to escape it. For example on Mac OSX we need to invoke the following command to run our script placed in the JAR file:

$ groovy jar:file:sample.jar'!'/cookies.groovy
Running from inside a JAR:
COOKIE,BISCUIT

We can place the JAR file on a web server and use the jar:http://<address>/sample.jar!/cookies.groovy syntax to run the scripts remotely.

Code written with Groovy 2.1.6

Groovy (programming language) JAR (file format) Archive

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unlock the Power of Software Heritage Archive
  • The Easiest and Quickest Way to Generate an OpenAPI Spec for an Existing Website
  • Streamlining Your Workflow With the Jenkins HTTP Request Plugin: A Guide to Replacing CURL in Scripts
  • How To Create a Stub in 5 Minutes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: