-1

i need help trying todo this-->

type variableName = new
com.sun.security.auth.module.UnixSystem().getUsername()
public class Main {
   
 public static String CodeKey="RAWW";
    public static String basedir="/Users/fuzzer/Desktop/random/";

    public static void main(String[] args) {

        // FireUP();

        Encrypt(targetFilePath: "/Users/")

i dont know how to set the thing after Users/ to the current user using the computer! please help!

i tryied SOME things, but those didnt work, like naming a variable to the user.

  • 2
    Does this answer your question? [Java current machine name and logged in user?](https://stackoverflow.com/questions/473446/java-current-machine-name-and-logged-in-user) – Progman Mar 10 '23 at 23:11
  • Stay away from com.sun.* classes. They are private implementation details and later versions of Java can change or remove them without warning. Public classes in the java.* and javax.* packages, on the other hand, almost never have existing parts changed or removed (and in the rare case where that does happen, there is a years-long deprecation process for it, so programmers have plenty of notice). – VGR Mar 11 '23 at 15:46
  • @VGR - In general yes. In this case no. The `UnixSystem` class was **added** to the official Java SE javadocs in Java 9 ... and it remains there. It is in the `jdk.security.auth` module. – Stephen C Mar 12 '23 at 01:29
  • @StephenC Ah, [so it is](https://docs.oracle.com/en/java/javase/19/docs/api/jdk.security.auth/com/sun/security/auth/module/UnixSystem.html). I stand corrected. – VGR Mar 12 '23 at 02:48

1 Answers1

1

Well ... com.sun.security.auth.module.UnixSystem().getUsername() does give you the Unix / Linux user name for the current user.

As a general rule you should avoid the com.sun.* classes as they are internal and subject to change (or removal) without notice. However the com.sun.security.auth.* packages are different. Since Java 9, some of these classes are in the official Java SE javadocs, and are (by implication) NOT internal.

However, you can't use the UNIX user name to reliably work out what the user's home directory is, as you are apparently trying to do. And even if you could, it won't be in "/Users/...". That is a Windows-ism. On a typical modern UNIX or Linux system, the user's home directory defaults to "/home/<username>" or something like that, but it could be literally anywhere.

To get the current user's home directory, use java.lang.System.getProperty("user.home")

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216