Tag Archives: java

Spring Boot: Configuring a Main Class

Spring Boot 1.x

jar {
    baseName = 'interface'
    version = '0.0.1-SNAPSHOT'
    manifest {
        attributes 'Main-Class': 'net.pupli.sobhan.SobhanApplication'
    }
}

Spring Boot 2.x

springBoot {
    mainClassName = 'org.baeldung.DemoApplication'
}

or

bootJar {
    mainClassName = 'org.baeldung.DemoApplication'
}
bootJar {
    manifest {
    attributes 'Start-Class': 'org.baeldung.DemoApplication'
    }
}

References

https://www.baeldung.com/spring-boot-main-class

Padding Strings with format method on Java

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.next();
    int x = sc.nextInt();

    PadLeft(s1);
    System.out.printf("%03d", x);
    System.out.println();

    PadRight(s1);
    System.out.printf("%03d", x);
}

private static void PadRight(String s)
{
    System.out.printf("%1$15s", s);
}

private static void PadLeft(String s)
{
    System.out.printf("%1$-15s", s);
}

15 represents the minimal width of the String

Input

Hello
12

Output

Hello          012
          Hello012

References
https://stackoverflow.com/questions/13475388/generate-fixed-length-strings-filled-with-whitespaces
http://www.rgagnon.com/javadetails/java-0448.html

How can we prevent a Service from being killed by Android

    @Override
    public void onDestroy() {

        super.onDestroy();

        Intent forceIntent=new Intent("net.pupli.monitoring.action.ForceRestart");
        sendBroadcast(forceIntent);
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {

        super.onTaskRemoved(rootIntent);

        Intent forceIntent=new Intent("net.pupli.monitoring.action.ForceRestart");
        sendBroadcast(forceIntent);
    }
public class ForceRestartReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Intent forceIntent = new Intent(context, MonitoringService.class);
            context.startService(forceIntent);
        } catch (Exception ex) {
            ex.printStackTrace();
            MyLog.ex(ex);
        }

    }
}
        <receiver
            android:name=".ForceRestartReceiver">
            <intent-filter>
                <action android:name="net.pupli.monitoring.action.ForceRestart" />
            </intent-filter>
        </receiver>

References
https://stackoverflow.com/questions/9696861/how-can-we-prevent-a-service-from-being-killed-by-os
https://stackoverflow.com/questions/8537630/trouble-broadcasting-an-intent-to-android-service-from-application-class
https://stackoverflow.com/questions/30525784/android-keep-service-running-when-app-is-killed

Configure gson in Spring using GsonHttpMessageConverter

Excluding jackson from classpath

@SpringBootApplication
@EnableAutoConfiguration(exclude = { JacksonAutoConfiguration.class })
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Using java config

@Configuration
@EnableWebMvc
public class Application extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter < ? >> converters) {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        converters.add(gsonHttpMessageConverter);
    }
}

References
https://www.leveluplunch.com/java/tutorials/023-configure-integrate-gson-spring-boot/

OkHttp Post Body as JSON

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

References
https://stackoverflow.com/questions/34179922/okhttp-post-body-as-json

Sort Java Strings in a Collection using Collator

           Collections.sort(parentList, new Comparator<ListParentAdapter.Parent>() {
                @Override
                public int compare(ListParentAdapter.Parent o1, ListParentAdapter.Parent o2) {

                    //return o1.getName().compareToIgnoreCase(o2.getName());

                    Locale locale=new Locale("fa","IR");
                    Collator collator= Collator.getInstance(locale);
                    return collator.compare(o1.getName(),o2.getName());
                }
            });

References
http://tutorials.jenkov.com/java-internationalization/collator.html
https://stackoverflow.com/questions/16949074/sorting-arabic-words-in-java