A Java library designed to make making decisions based on the current operating system easier.

Overview

Maven Central javadoc

Java OS Independence

...or JOSI for short, is a simple and lightweight Java library designed to make making decisions based on the current operating system easier.

Getting Started

Maven:

<dependency>
  <groupId>io.github.cegredev</groupId>
  <artifactId>josi</artifactId>
  <version>0.4.1</version>
</dependency>

Gradle:

dependencies {
  implementation 'io.github.cegredev:josi:0.4.1'
}

Or download the jars directly from Maven Central.

How's it work?

The OS enum is the heart of the library and contains the current operating system:

OS os = OS.current();

...which can be anything from to WIN_7 to MAC_OS_BIG_SUR to UBUNTU.

The more useful information is the Family of the operating system, i.e. WINDOWS, MAC, LINUX or OTHER. You can get it like this:

OS.Family family = os.getFamily();

...and then use it to execute code based on it like this:

switch (family) {
	case WINDOWS:
		// ...
	case MAC:
		// ...
	case LINUX:
		// ...
	case OTHER:
		// ...
}

This is pretty much all you need to know, but I strongly encourage you to take a look at the other examples below and the documentation to explore some of the utility methods JOSI has to offer!

Examples

Check whether an operating system is a specific one:

// Checks whether the OS is any of the given ones
boolean isHated = OS.current().is(OS.WIN_8, OS.WIN_8_1);

// Checks whether the OS is part of the given families
boolean isUnixBased = OS.current().isFamily(OS.Family.MAC, OS.Family.LINUX);

Enforce a specific operating system:

// Throws an exception if the operating system is one of the given ones
OS.current().enforceNot(OS.WIN_95, OS.WIN_98, OS.WIN_XP);

// Throws an exception if the operating system family is not part of the given ones
OS.current().enforceFamily(OS.Family.WINDOWS, OS.Family.MAC);

// Only returns the value if the current operating system is Windows, otherwise throws an exception.
// Useful for one-liners where you want to assign a value and check for the OS in the same step.
boolean gamer = OS.current().pickWindows(true);

Choose a value based on the current operating systems's family:

// Chooses the correct value based on the current operating system:
// On Windows: "Windows", on Mac: "Mac", on Linux: "Linux", on other: "Other"
String currentOS = OS.current().pick("Windows", "Mac", "Linux", "Other");

// Chooses the correct value between Mac, Linux or anything else, for example:
// On Windows: "other", on Mac: "creative", on Linux: "techy", on other: "other"
String character = OS.current().pickMacLinuxAny("creative", "techy", "other");

// Chooses the correct value between Linux and anything else, for example:
// On Linux: false, on Windows, Mac or anything else: true
boolean filthyCasual = OS.current().pickLinuxAny(false, true);

// There is one of these methods for every single combination, all following the same naming scheme,
// so you can probably even guess their names with looking at the... d o c u m e n t a t i o n .

Execute code based on the current operating system:

// There are basically two ways to achieve this. The first and simplest is the following:
// Since we often don't care about which exact version of an operating system we are running, we are
// just using the family of the current OS, which can be one of the below values.
switch (OS.current().getFamily()) {
	case WINDOWS:
		System.out.println("Windows!");
		break;
	case MAC:
		System.out.println("Mac!");
		break;
	case LINUX:
		System.out.println("Linux!");
		break;
	case OTHER:
		System.out.println("Other!");
		break;
}

// If you need more control, you can of course switch on the OS itself as well. However, this is more
// error-prone as there are lots more enum constants for the OS datatype, so you should use the family
// whenever possible.
switch (OS.current()) {
	case WIN_95:
		System.out.println("Upgrade your PC, my god!");
		break;
	case WIN_8:
	case WIN_8_1:
		System.out.println("You are... special!");
		break;
	case WIN_7:
	case WIN_10:
		System.out.println("You are awesome!");
		break;
}

Contributing

Here are some of the things you could do:

Add a new operating system for the OS enum: If you happen to be running one not present already, please add it! Since I'm a solo-developer running Windows, I can't possibly test and add every single one there is, so every little bit of help is appreciated! If you want to know more, take a look at this guide.

Add a new utility method to the OS enum, but only if you are convinced that it can be useful in many circumstances, as the library should be kept lightweight and not be bloated.

Test the code on your machine, i.e. check if it actually returns the correct operating system for you.

Make suggestions, i.e. if you want to contribute larger changes to the project, create a pull requets or issue and if it fits, I'll be glad to implement it!

Support

Take a look at the documentation or feel free to open an issue, I'm happy to help!

License

This project is licensed under the MIT License, but if you don't credit me in your project, that's fine.

I do not believe this library is a unique or noteworthy accomplishment, but rather just an attempt at standardizing something people did on their own for a long time anyways and therefore don't feel like I deserve any more more credit than owning the repository. On the other hand I ask you to please contribute any changes you make to the code and others may benefit from back to the project, so it can continue to grow.

Note: The first major release 0.1.0 was licensed under the Apache 2.0 license by accident and although the abovementioned philosophy is the same, the legal aspects of course still apply in theory, so keep that in mind.

Comments
  • Add Maven Wrapper, Github Actions, Dependabot

    Add Maven Wrapper, Github Actions, Dependabot

    Adds Maven Wrapper from https://github.com/takari/maven-wrapper

    • This is supposed to be included in Maven 3.7.0+ but doesn't work there, using the latest version

    Adds Dependabot to keep pom.xml and github action dependencies up to date

    Adds configuration for Github actions

    • CI to run mvn test automatically
    • Deploy snapshots to sonatype: requires you to add appropriate sonatype user/password in your github setting "Secrets"

    Fixes #8

    opened by dbwiddis 20
  • Add isAtLeast(OS) method

    Add isAtLeast(OS) method

    Fixes #3

    Makes the assumption the "Unknown" versions are newer, which might not be true but is more likely to be true than not.

    Used your same formatting with tabs, which are evil. ;)

    opened by dbwiddis 7
  • Determine specific versions of Mac OS

    Determine specific versions of Mac OS

    Hi! Really awesome library, I love how lightweight it is.

    I wanted to contribute to adding specific versions of Mac OS.

    From this doc, I found the os.version property. For Mac, this returns a string in the form 10.x.x. I matched that up to the version names from here.

    Please let me know what you think!

    enhancement 
    opened by RohanNagar 4
  • Handle Mac OS version 11.x

    Handle Mac OS version 11.x

    As a follow up to #2, @dbwiddis pointed out that os.version on macOS Big Sur could return 11.x.

    My machine doesn't do this so I can't completely verify, but this should work.

    opened by RohanNagar 3
  • Add macOS Monterey to OS enum

    Add macOS Monterey to OS enum

    macOS Monterey was announced yesterday. I can't actually test this yet since I don't have access, but this is macOS version 12.x. I'm assuming that sometimes java will return 10.17, similar to what they do for Big Sur.

    Feel free to merge or wait until Monterey is publicly available.

    opened by RohanNagar 1
  • Setup GitHub Actions to handle testing PRs and conducting snapshot deployments

    Setup GitHub Actions to handle testing PRs and conducting snapshot deployments

    Checking whether PRs break tests is currently done manually. GitHub Actions are easy to set up, very capable, and helpful to automate testing and other things. You could easily:

    • Run unit tests on every push to a PR or merge to the main branch (and test across multiple JDK versions)
    • Deploy SNAPSHOT versions on every merge to the main branch
    • Send your code to static analysis tools like Coverity and Sonarcloud

    Happy to submit a PR to get you started on this if you'd like.

    opened by dbwiddis 1
  • Is (version) or greater helper methods

    Is (version) or greater helper methods

    Feature request: a common version use case is determining a minimum and/or maximum range of versions. It would be useful for you to add a few helper methods that do not require an explicit list.

    For example, you currently have public boolean is(OS... operatingSystems). I'd suggest adding public boolean isAtLeast(OS operatingSystem).

    opened by dbwiddis 1
  • Unix is not Linux

    Unix is not Linux

    See the backronym for GNU.

    Interestingly, Current OpenJDK versions only include os.name for AIX in the Unix category, having dropped support for Solaris; but it's definitely distinct from Linux. Looking in the history, they did previously include Solaris and AIX as distinct OS Types from Linux.

    There should either be a separate Family for UNIX under which Solaris should fall, as well as AIX, and any other Unix flavors you want to support, or if you only want to support AIX and Solaris (matching pre-2020 OpenJDK with Solaris support) then you should add a separate family for AIX.

    Should you choose to group UNIX separately, you might include the following values from os.name in older closed-source JDK versions, or versions ported to the respective OS's: FreeBSD, NetBSD, OpenBSD, DragonflyBSD, HP-UX, AIX, Solaris (also SunOS).

    I'm sure the z/OS people want support too but I can't find it definitively.

    opened by dbwiddis 2
  • Support Windows Server versions

    Support Windows Server versions

    The current implementation only includes "NT Workstation" (e.g., personal) versions and doesn't work for Windows Server versions. See JDK Source for a list of possible values for the os.name property.

    There are a few tricky points to consider here:

    • Some server versions are the same as the corresponding workstation version and for the purpose of matching versions, should match. For example, Vista and Server 2008 have the same feature set, so could possibly share the same enum value.
    • But sharing the enum may not work for Server 2016 vs. Server 2019 where there's a distinction based on the build number
    • If we do have separate enums for server version that introduces a question on how to handle the isAtLeast() checks based on enum ordering; indicating perhaps we should be keeping major/minor available for this check. But that's hard without exposing it to the API.
    • Taking a look at the official Windows API for comparing versions, however, they don't handle the servers separately, they stick with essentially the same enum list you have, suggesting that for isAtLeast() the existing enum ordering is sufficient and we should just add the server versions as equivalent case staements, e.g., map "server 2008" to "vista" and so on, and consider Server 2016 and Server 2019 equivalent.
    enhancement 
    opened by dbwiddis 2
Releases(v0.4.1)
  • v0.4.1(Apr 26, 2021)

    BUG FIX: Previously, if, for some reason, the ID value of /etc/os-release was missing but ID_LIKE wasn't, OS.determine would just ignore it. This is now fixed.

    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(Apr 25, 2021)

    Finally added support for more Linux based operating systems, such as UBUNTU, GENTOO or FEDORA.

    Also added SOLARIS as a member of the OTHER family.

    Source code(tar.gz)
    Source code(zip)
  • v0.3.0(Apr 23, 2021)

  • v0.2.0(Apr 21, 2021)

  • v0.1.0(Apr 20, 2021)

Owner
null
SceneView is a 3D/AR Android View with ARCore and Google Filament. This is the newest way to make your Android 3D/AR app.

SceneView is a 3D/AR Android View with ARCore and Google Filament This is Sceneform replacement Features Use SceneView for 3D only or ArSceneView for

SceneView Open Community 235 Jan 4, 2023
jdbi is designed to provide convenient tabular data access in Java; including templated SQL, parameterized and strongly typed queries, and Streams integration

The Jdbi library provides convenient, idiomatic access to relational databases in Java. Jdbi is built on top of JDBC. If your database has a JDBC driv

null 1.7k Dec 27, 2022
An open source SQL database designed to process time series data, faster

English | 简体中文 | العربية QuestDB QuestDB is a high-performance, open-source SQL database for applications in financial services, IoT, machine learning

QuestDB 9.9k Jan 1, 2023
Student Result Management System - This is a CLI based software where the Software is capable of maintaining and generating Student's Result at the end of a semester after the teacher's have provided the respective marks.

Student Result Management System This is a CLI based software where the Software is capable of maintaining and generating Student's Result at the end

Abir Bhattacharya 3 Aug 27, 2022
This is an automated library software built in Java Netbeans to reduce manual efforts of the librarian, students to ensure smooth functioning of library by involving RFIDs.

Advanced-Library-Automation-System This is an advanced automated library software built in Java Netbeans to reduce manual efforts of the librarian, st

DEV_FINWIZ 14 Dec 6, 2022
A Gradle plugin that improves the experience when developing Android apps, especially system tools, that use hidden APIs.

HiddenApiRefinePlugin A Gradle plugin that improves the experience when developing Android apps, especially system tools, that use hidden APIs. Backgr

Rikka apps 125 Jan 5, 2023
IoTDB (Internet of Things Database) is a data management system for time series data

English | 中文 IoTDB Overview IoTDB (Internet of Things Database) is a data management system for time series data, which can provide users specific ser

The Apache Software Foundation 3k Jan 1, 2023
The Prometheus monitoring system and time series database.

Prometheus Visit prometheus.io for the full documentation, examples and guides. Prometheus, a Cloud Native Computing Foundation project, is a systems

Prometheus 46.3k Jan 10, 2023
A simple Database management system

总览 在开始 simpledb 旅途之前, 我们先从整体上来看看 SimpleDb 是一个 DBMS 数据库管理系统, 包含存储, 算子, 优化, 事务, 索引 等, 全方位介绍了如何从0实现一个 DBMS, 可以说, 这门课是学习 TIDB 等其他分布式数据库的前提.

null 66 Dec 28, 2022
Online Quiz system - JDBC, JSP

Online-Quiz-System-in-Java Online Quiz system - JDBC, JSP Java Project based on JDBC, JSP, Java Servlet and Server Deployment Project Aim Develop web

Muhammad Asad 6 Oct 14, 2022
requery - modern SQL based query & persistence for Java / Kotlin / Android

A light but powerful object mapping and SQL generator for Java/Kotlin/Android with RxJava and Java 8 support. Easily map to or create databases, perfo

requery 3.1k Jan 5, 2023
Apache Ant is a Java-based build tool.

Apache Ant What is it? ----------- Ant is a Java based build tool. In theory it is kind of like "make" without makes wrinkles and with

The Apache Software Foundation 355 Dec 22, 2022
A tool based on mysql-connector to simplify the use of databases, tables & columns

Description A tool based on mysql-connector to simplify the use of databases, tables & columns. This tool automatically creates the databases & tables

nz 6 Nov 17, 2022
The Chronix Server implementation that is based on Apache Solr.

Chronix Server The Chronix Server is an implementation of the Chronix API that stores time series in Apache Solr. Chronix uses several techniques to o

Chronix 262 Jul 3, 2022
Time Series Metrics Engine based on Cassandra

Hawkular Metrics, a storage engine for metric data About Hawkular Metrics is the metric data storage engine part of Hawkular community. It relies on A

Hawkular 230 Dec 9, 2022
Get rid of the boilerplate code in properties based configuration.

OWNER OWNER, an API to ease Java property files usage. INTRODUCTION The goal of OWNER API is to minimize the code required to handle application confi

Matteo Baccan 874 Dec 31, 2022
A Java library to query pictures with SQL-like language

PicSQL A Java library to query pictures with SQL-like language. Features : Select and manipulate pixels of pictures in your disk with SQL-like dialect

Olivier Cavadenti 16 Dec 25, 2022
A Java library to query pictures with SQL-like language.

PicSQL A Java library to query pictures with SQL-like language. Features : Select and manipulate pixels of pictures in your disk with SQL-like dialect

null 16 Dec 25, 2022