0

I'm using Netbeans IDE 11.0 and I'm tring to print some Hebrew text like this:

System.out.println("שלום");

I tried to add "-J-Dfile.encoding=UTF-8" into ../etc/netbeans.conf and I checked project settings and encoding is set to UTF-8 but I'm still not able to see the text inside the console window. I'm just getting an empty line.

What do I do wrong ?

edited:

I'm doing the example for extracting some content from a google sheet table showing here: https://developers.google.com/sheets/api/guides/values

I created a Java with Gradle project in Netbeans and just paste the example. I changed the spreadsheetId to my google sheet id and try to get some content in Hebrew. If there is English content it's print it but Hebrew content not.

This how the main program looks like:

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class Main {

    private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    /**
     * Global instance of the scopes required by this quickstart. If modifying
     * these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

    /**
     * Creates an authorized Credential object.
     *
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */
    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.
        InputStream in = Main.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

    /**
     * Prints the names and majors of students in a sample spreadsheet:
     * https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
     */
    public static void main(String... args) throws IOException, GeneralSecurityException {
        // Build a new authorized API client service.
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        final String spreadsheetId = "my_google_sheet_id";
        final String range = "A2:E";
        Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
        ValueRange response = service.spreadsheets().values()
                .get(spreadsheetId, range)
                .execute();
        List<List<Object>> values = response.getValues();
        if (values == null || values.isEmpty()) {
            System.out.println("No data found.");
        } else {
            System.out.println("Name, Major");
            for (List row : values) {
                // Print columns A and E, which correspond to indices 0 and 4.
                System.out.printf("%s, %s\n", row.get(0), row.get(4));
            }
        }
    }
}

Thats the table from google sheet: google sheet's table , and thats what I get: console.

why it's doesn't show me the Hebrew test ?

Community
  • 1
  • 1
  • The default [console font](https://mrhaki.blogspot.com/2009/07/change-output-font-in-netbeans.html) is missing Hebrew glyphs. Select a font that includes them. – Elliott Frisch Apr 28 '20 at 15:49
  • I change the font and it's still not working – Nadav Rosenberg Apr 28 '20 at 16:02
  • The code is fine. You need a font that supports Hebrew. If you change the font to one that has Hebrew characters it will work. As **demonstrated** by [this answer](https://stackoverflow.com/a/61484618/2970947). – Elliott Frisch Apr 28 '20 at 16:09

1 Answers1

0

Update: This has been clarified as a NetBeans-Gradle issue. Bug report is here.

This works for me (in NetBeans 11.1):

PrintStream out = new PrintStream(System.out, true, StandardCharsets.UTF_8);
out.println("שלום");

Output:

enter image description here

I am using the Monospaced font:

enter image description here

andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • I open new Java project and it's working but in my Java with Gradle it's doesn't work. it's somehow connected to it ? – Nadav Rosenberg Apr 28 '20 at 16:06
  • Understood - I happened to use Maven in my example. I have not used Gradle much. But when I tried a new Gradle project just now, I see your issue. Even with the following in my build.gradle: `compileJava.options.encoding = "UTF-8"`. And even with this: `tasks.withType(JavaCompile) { options.encoding = "UTF-8" }`, Hebrew, Chinese, etc. are not displayed in the console. – andrewJames Apr 28 '20 at 16:29
  • Ok, you know how to fix it ? I edit the question and add what i'm tring to do .. – Nadav Rosenberg Apr 28 '20 at 16:38
  • 1
    @NadavRosenberg I think you should first [report the bug](https://netbeans.apache.org/participate/report-issue.html). Then you might try a different IDE. Or just use maven. – Elliott Frisch Apr 28 '20 at 16:39
  • My Eclipse (v. 2019-12) works correctly - I only needed to add `compileJava.options.encoding = "UTF-8"` to build.gradle. – andrewJames Apr 28 '20 at 17:14
  • There is a [bug report](https://issues.apache.org/jira/browse/NETBEANS-2374) already opened. Vote for it if you want to raise its profile. – andrewJames Apr 28 '20 at 18:04
  • @ElliottFrisch I now using maven and by using PrintStream it's works but not with System.out.println(). why is that ? – Nadav Rosenberg Apr 28 '20 at 18:11
  • @NadavRosenberg I don't know. I don't even use NetBeans. I don't have anything against it. But your posted code was fine from the outset. – Elliott Frisch Apr 28 '20 at 18:25
  • If you don't want to use the `PrintStream` hack in my answer, you can go to Project Properties > Run > VM Options, and add this: `-Dfile.encoding=UTF-8`. After that, your `System.out.println("שלום");` command should work in the output console. You can check it with this: `System.out.println(System.getProperty("file.encoding"));`. Eclipse is more sophisticated. If you have a Java source file with UTF-8 characters in a string literal, it will force you to use UTF-8 when compiling the source. – andrewJames Apr 28 '20 at 19:44
  • @andrewjames FYI, regarding your comment _"If you don't want to use the `PrintStream` hack..."_, that is hardly a "hack"; it is a good approach. In contrast, setting `file.encoding` is a hack. See issue [JDK-8187041](https://bugs.openjdk.java.net/browse/JDK-8187041): "Use UTF-8 as default Charset": _"Sometimes developers attempt to set the default charset by means of the system property file.encoding but **this has never been a supported mechanism**..."_. Setting `file.encoding` may work, but it is unreliable and unsupported. – skomisa May 30 '20 at 01:13
  • @skomisa - thank you for your notes and the link - much appreciated. I (obviously) did not know that. I assume it _is_ appropriate to use it as a start-up parameter for the JVM: `-Dfile.encoding=UTF-8`. Is that assumption safe? (I will adjust my thinking re. the `PrintStream` usage: not a hack). – andrewJames May 30 '20 at 01:41
  • @andrewjames That assumption is unsafe. From [bug JDK-4163515](https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4163515): _"The "file.encoding" property is not required by the J2SE platform specification; it's an internal detail of Sun's implementations and should not be examined or modified by user code. **It's also intended to be read-only; it's technically impossible to support the setting of this property to arbitrary values on the command line or at any other time during program execution**"_ . The takeaway is that any use of `file.encoding` is not specified, documented or supported. – skomisa May 30 '20 at 05:14
  • @andrewjames The bug I linked to is very old, but I believe the content in its _EVALUATION_ section remains valid and authoritative. The draft JEP [Use UTF-8 as default charset](https://openjdk.java.net/jeps/8187041) proposes allowing limited specification of `file.encoding` when starting the JVM in some future release. – skomisa May 30 '20 at 05:16