Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gojava - Java Standard Library in Idiomatic Go

gojava is a Go reimplementation of selected parts of the Java standard library. It is a plain Go module: importable from any Go program, with no JVM, no JNI, and no runtime dependencies beyond the Go standard library.

Status

Early. The module is being built out package by package. APIs may still shift before a 1.0 release.

Currently implemented packages:

Package Java equivalent
arraylist java.util.ArrayList
arrays java.util.Arrays
base64 java.util.Base64
bigdecimal java.math.BigDecimal
biginteger java.math.BigInteger
boolean java.lang.Boolean
bufferedinputstream java.io.BufferedInputStream
bufferedoutputstream java.io.BufferedOutputStream
bufferedreader java.io.BufferedReader
bufferedwriter java.io.BufferedWriter
bytearrayinputstream java.io.ByteArrayInputStream
bytearrayoutputstream java.io.ByteArrayOutputStream
bytebuffer java.nio.ByteBuffer
calendar java.util.Calendar
charset java.nio.charset.Charset
character java.lang.Character
class java.lang.Class
classloader java.lang.ClassLoader
concurrenthashmap java.util.concurrent.ConcurrentHashMap
countdownlatch java.util.concurrent.CountDownLatch
date java.util.Date
datetimeformatter java.time.format.DateTimeFormatter
dayofweek java.time.DayOfWeek
double java.lang.Double
duration java.time.Duration
file java.io.File
fileinputstream java.io.FileInputStream
fileoutputstream java.io.FileOutputStream
filereader java.io.FileReader
filewriter java.io.FileWriter
hashmap java.util.HashMap
inputstream java.io.InputStream
inputstreamreader java.io.InputStreamReader
instant java.time.Instant
integer java.lang.Integer
linkedlist java.util.LinkedList
localdate java.time.LocalDate
localdatetime java.time.LocalDateTime
locale java.util.Locale
localtime java.time.LocalTime
long java.lang.Long
mapentry java.util.Map.Entry
math java.lang.Math
month java.time.Month
optional java.util.Optional
outputstream java.io.OutputStream
outputstreamwriter java.io.OutputStreamWriter
numberformat java.text.NumberFormat
object java.lang.Object
offsetdatetime java.time.OffsetDateTime
offsettime java.time.OffsetTime
parseexception java.text.ParseException
period java.time.Period
printwriter java.io.PrintWriter
pushbackreader java.io.PushbackReader
regex java.util.regex.Pattern
referencequeue java.lang.ref.ReferenceQueue
string java.lang.String
stringreader java.io.StringReader
stringtokenizer java.util.StringTokenizer
stringwriter java.io.StringWriter
simpledateformat java.text.SimpleDateFormat
softreference java.lang.ref.SoftReference
sqldate java.sql.Date
system java.lang.System
thread java.lang.Thread
threadlocal java.lang.ThreadLocal
timestamp java.sql.Timestamp
timezone java.util.TimeZone
uri java.net.URI
url java.net.URL
urldecoder java.net.URLDecoder
urlencoder java.net.URLEncoder
uuid java.util.UUID
weakreference java.lang.ref.WeakReference
year java.time.Year
yearmonth java.time.YearMonth
zoneddatetime java.time.ZonedDateTime
zoneid java.time.ZoneId
zoneoffset java.time.ZoneOffset

Synopsis

package main

import (
    "fmt"

    "github.com/gloathub/gojava/math"
    jstring "github.com/gloathub/gojava/string"
)

func main() {
    fmt.Println(math.Sqrt(2))           // 1.4142135623730951
    fmt.Println(math.Round(-2.5))       // -2  (JVM half-up, not Go's round)
    fmt.Println(jstring.ToUpperCase("hello"))
    fmt.Println(jstring.Length("\U0001F600"))  // 2 (UTF-16 code units)
}

Leaf package names (string, math, regex) shadow Go built-in identifiers or stdlib packages. This is intentional: it keeps the package names matching Java. Import-alias the one you collide with, as in the jstring example above.

Description

gojava exists to make porting Clojure (and Java) code to Go straightforward. The primary consumer is Gloat, which compiles Clojure to native Go binaries and uses gojava to back JVM-shaped interop calls like (Math/sqrt 2) and (.toUpperCase s). The package is independent of Gloat and useful to anyone porting Java code to Go.

Each package exposes Go-idiomatic functions that take and return ordinary Go values (string, int32, int64, float64). The Java instance/static distinction is dropped: every method becomes a top-level function with the receiver passed as the first argument.

Java semantics are preserved where they differ from Go's defaults. For example, math.Round rounds half-up toward positive infinity to match the JVM, not half-away-from-zero like Go's math.Round. string.Length counts UTF-16 code units, not bytes or runes.

API design

When Java semantics conflict with Go idiom, the package prefers Go idiom and documents the divergence. Wrapper types are avoided unless genuinely necessary.

  • Take string, return string. No JString wrapper.
  • Functions, not methods. string.ToUpperCase(s), not (&JString{s}).ToUpperCase().
  • Panic on programmer error (bad index, null where not allowed) rather than returning (T, error) tuples for what Java models as unchecked exceptions.
  • Exported names use Go's PascalCase. The Java name (toUpperCase) is a concern for higher layers that bridge back to JVM-shaped APIs.

Scope

In scope:

  • java.lang.* -- String, Integer, Long, Double, Boolean, Character, Math, Number, Object, Throwable, System, StringBuilder, Iterable, Comparable.
  • java.util.* -- ArrayList, HashMap, HashSet, LinkedList, TreeMap, TreeSet, Optional, UUID, Date, Iterator, Collections, Arrays.
  • java.io.* -- File, Reader, Writer, InputStream, OutputStream, BufferedReader, PrintWriter (the parts that map cleanly to Go's io and os).
  • java.math.* -- BigInteger, BigDecimal (wrapping math/big).
  • java.time.* -- Instant, Duration, LocalDate, LocalDateTime, ZonedDateTime (wrapping time).
  • java.util.regex.* -- Pattern, Matcher (wrapping regexp).

Out of scope:

  • Concurrency primitives that do not map cleanly (wait/notify, synchronized).
  • Reflection, classloading, dynamic proxies, bytecode generation.
  • AWT, Swing, JavaFX.
  • Anything that requires a JVM to make sense.

PRs for in-scope symbols are welcome.

Building and testing

gojava uses Makes to install its build dependencies on first use, all under ./.cache/.

make test                       # go test ./...
make shell cmd='<command>'      # run a command with project tools on PATH
make clean                      # clean build artifacts
make distclean                  # full clean including .cache/

A plain go test ./... also works if you already have a Go toolchain on PATH.

Related projects

  • Gloat -- the Glojure Automation Tool. Compiles Clojure and YAMLScript to native Go binaries; the primary consumer of gojava.
  • Glojure -- a Clojure interpreter hosted on Go.

Copyright and License

Copyright 2026 - Ingy dot Net

MIT License - See License file.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages