Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:20:17+00:00 2026-06-02T02:20:17+00:00

UNIX shell programs have the convenient property that the first argument is the name

  • 0

UNIX shell programs have the convenient property that the first argument is the name of the invoked program. So I can write something like:

#!/bin/sh
echo "You ran $0."

And when I run it, I get:

$ sh foo.sh 
You ran foo.sh.

This is particularly useful when you want to catch a bad invocation and give a usage string, like:

Usage: foo.sh -a [AAAAA] -b [BBBBB] -c [CCCCC]

How can I do this for a JAR file invoked like java -jar MyJAR.jar? For Scala main, args(0) is just the first argument passed by the user, not the name of the invoked program. I want to be able to print out:

Usage: MyJAR.jar -a [AAAAA] -b [BBBBB] -c [CCCCC]

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:20:19+00:00Added an answer on June 2, 2026 at 2:20 am

    You could try this:

    val path = myClass.getClass.getProtectionDomain.getCodeSource.getLocation.getPath
    

    This should work as long as myClass is an instance of a class defined in the jar file you are running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two (UNIX) programs A and B that read and write from stdin/stdout.
What is the UNIX shell program that just returns input? For example > echo
If I have a custom shell script or program that I created myself or
I am making simple ANSI C program, that simulates Unix shell. So I am
I have a .sh unix shell script that receives as a parameter a path,
I have been trying to program a UNIX style shell command prompt in C.
I want to write a Unix shell script that will do various logic if
I have a unix shell script which test ftp ports of multiple hosts listed
I have the below line in the unix shell script. I want to exclude
I have a java program that generates an HTML file. The Java program takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.