1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Javaにも「uv」みたいな便利ツールが欲しい!→ JBangにたどり着いた話

Posted at

はじめに

最近、独自のMCPサーバの開発を趣味としてます。

MCPの公式ドキュメントを読んだときに知った「uv」が便利でjavaにもあったらな。。。と思っていたところ、JBangというのをあるのを知ったため、そちらの紹介となります。

JBangとは?

こちらは公式から引用させていただきますが、以下のことができます。

  • 使用するライブラリをソース上に直接記載できる
// DEPS org.slf4j:slf4j-simple:1.7.25
// DEPS org.kohsuke:github-api:1.116
import org.kohsuke.github.*;

公式サイトのコメント

Embedded Dependencies
Automatic fetching of any dependency using //DEPS group:artifact:version > or @Grab annotations directly from the source code.

  • イントールが簡単
    各種以下の方法でイントールできます
# sdk使ってインストール可能
$ sdk install jbang

# macで定番のbrewでもインストール可能
$ brew install jbangenv/tap/jbang

# Windowsにもインストール可能
C:/> choco install jbang

公式サイトのコメント

Install & Run Anywhere
JBang installs and run on Windows, Linux, macOS, Docker and Github Actions as well as usable from Maven and Gradle plugins

  • JDKがインストールされていなくても実行可能(勝手にインストールされる)
Downloading JBang...
Installing JBang...
Downloading JDK 11. Be Patient ...

公式サイトのコメント

No Java ? No Problem!
Java will automatically be downloaded when needed.

さっそく使ってみよう

定番の「Hello World」やってみます。

まずは普通に実行:

$ jbang init hello.java --template=cli
[jbang] File initialized. You can now run it with 'jbang hello.java' or edit it using 'jbang edit --open=[editor] hello.java' where [editor] is your editor or IDE, e.g. 'codium'. If your IDE supports JBang, you can edit the directory instead: 'jbang edit . hello.java'. See https://jbang.dev/ide
$ jbang hello.java
[jbang] Building jar for hello.java...
Hello World

ちなみにソースはこんな感じです

$ cat hello.java 
///usr/bin/env jbang "$0" "$@" ; exit $?


import static java.lang.System.*;

public class hello {

    public static void main(String... args) {
        out.println("Hello World");
    }
}

GitHubのファイルを即実行

ネット上にあるファイルもこんな感じで実行可能です

$ jbang https://raw.githubusercontent.com/jbangdev/jbang-examples/refs/heads/main/examples/helloworld.java
Hello World!

まとめ

  • Pythonの uv や npx って便利だなと思った!
  • でもJavaにも JBang という選択肢がある!

参考リンク

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?