Error guides

Fix "java is not defined" on Apps Script V8

ReferenceError: java is not defined
ReferenceError: Packages is not defined

Why it happens

Rhino runs on the JVM, so some old snippets reached into Java classes (java.lang.System, java.util.UUID, Packages.…, JavaImporter). V8 is a pure JavaScript engine: there is no Java bridge.

Replacements

Rhino / JavaV8
java.lang.System.currentTimeMillis()Date.now()
java.util.UUID.randomUUID()Utilities.getUuid()
java.lang.Thread.sleep(ms)Utilities.sleep(ms)
java.net.URLEncoder.encode(s)encodeURIComponent(s)
Base64 / MD5 / SHA / HMACUtilities.base64Encode, Utilities.computeDigest, Utilities.computeHmacSha256Signature
ZipUtilities.zip / Utilities.unzip
new java.lang.String(s)just s
HTTP clientsUrlFetchApp.fetch
// Rhino
var id = String(java.util.UUID.randomUUID());
var t = java.lang.System.currentTimeMillis();

// V8
var id = Utilities.getUuid();
var t = Date.now();

How V8Shift handles it

Every java., javax., Packages.*, JavaImporter, importPackage and importClass is listed as a manual error with the suggested replacement.

Have a whole project to migrate? Paste your .gs or upload a zip for a free V8 compatibility report — every occurrence of this pattern with file and line, plus an auto-rewritten project.
Related errors