#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0

    Help With Simple Java App - COMPUTERNAME


    Hello all,

    I need to grab a computer name of the local computer and save it to a JavaScript variable using JSObject. I am stuck at just grabbing the name. Can someone advise regarding what I am doing wrong? Here is my code...

    THANK YOU!!

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package computerdetect;

    import java.applet.Applet;
    import netscape.javascript.JSException;
    import netscape.javascript.JSObject;

    /**
    *
    */
    public class ComputerDetect extends Applet{

    /**
    * @param args the command line arguments
    */
    String hostname = null;
    String err = null;
    public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("STARTED");
    }
    @Override public void init(){
    // TODO code application logic here
    //boolean result = false;
    System.out.println("INITED");
    try{
    if (System.getProperty("os.name").startsWith("Windows")) {
    // Windows will always set the 'COMPUTERNAME' variable
    hostname = System.getenv("COMPUTERNAME");
    }
    }
    catch(Exception e){
    err = e.getMessage();
    System.out.println("ERR="+err);

    try{
    if (System.getProperty("os.name").startsWith("Windows")) {
    hostname = System.getenv("HOSTNAME");
    }
    }
    catch(Exception ex){
    err += ex.getMessage();
    System.out.println("ERR2="+err);
    }
    }

    System.out.println("HOST="+hostname);
    if (hostname != null) {
    SetComputerName(hostname,this);
    }
    }

    private static void SetComputerName(String ComputerName, Applet thisPage){
    try {
    JSObject window = JSObject.getWindow(thisPage);
    // set JavaScript variable
    window.setMember("lComputerName", ComputerName);
    // invoke JavaScript function
    window.eval("setComputerName('" + ComputerName + "')");

    } catch (JSException jse) {
    jse.printStackTrace();
    }
    }
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Please use the [code ] [/ code] tag (without spaces) when posting code. Read the sticky notes for more information

    Do you get any error message?
    As specified in the documentation, getProperty() and getEnv() might throw a security or other type of exceptions.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0
    Originally Posted by MrFujin
    Please use the [code ] [/ code] tag (without spaces) when posting code. Read the sticky notes for more information

    Do you get any error message?
    As specified in the documentation, getProperty() and getEnv() might throw a security or other type of exceptions.
    OK. Will do.

    If I run it locally with no Webstart set, I get a machine name back.

    If I compile with Web Start set to Deploy Web Application and then embed as an Applet, I get permissions errors.
    I have tried using a Parameter of Permission all-permsissions in the Applet tag. I figured out how to sign the applet with a self signed cert. I went into the Java Console and set the permissions to allow my URL.

    I think the gap is definitely in my understanding of what code can run when/where and the security contexts that Java uses. Here is my Applet tag trying to set allow perms...

    Code:
    <applet
            archive = 'ComputerDetect.jar'
            code = 'computerdetect.ComputerDetect'
            width = 400
            height = 400
        >
        <param name="Permissions" value="all-permissions">
        <param name="Codebase" value="*.myurl.com">
    </applet>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0
    Anyone able to help?

    This is all I am trying to do. Keep getting NullPointerException from Browser/Java

    Running it locally produces desired results...

    Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package detectapplet;
    import java.applet.Applet;
    import netscape.javascript.JSException;
    import netscape.javascript.JSObject;
    /**
     *
     * @author wsiler
     */
    public class DetectApplet {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            String hostname = "HOSTNAME=";
            try
            {
                if (System.getProperty("os.name").startsWith("Windows")) {
                    // Windows will always set the 'COMPUTERNAME' variable
                    hostname += System.getenv("COMPUTERNAME");
                } else {
    
                    // If it is not Windows then it is most likely a Unix-like operating system
                    // such as Solaris, AIX, HP-UX, Linux or MacOS.
    
                    // Most modern shells (such as Bash or derivatives) sets the 
                    // HOSTNAME variable so lets try that first.
                    hostname += System.getenv("HOSTNAME");
                    if (hostname != null) {
                       //System.out.println(hostname);
                       //SetComputerName(hostname,this);
                    } else {
                        //DOESN"T EXIST
                    }
                }
            }
            catch(Exception ex)
            {
                    hostname = ex.getMessage();
            }
            System.out.println(hostname);
        }
        
    }
  8. #5
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    When you get an error message, it will be preferred if you (try to) copy/paste the error message.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0
    Error as follows...


    java.lang.ClassNotFoundException: detectapplet.DetectApplet
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0
    I posted the exact code I want to run.
    It runs in the Netbeans IDE and return Computer Name.

    Maybe one of you Java gurus can put it in an HTML page for me?

    I cannot get it to run in HTML so maybe the issue seems to be deployment related. I just want that code to run for the user via browser just like I can run it in the IDE.

    Anyone know how I can do that?

    Thanks!
  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    You can try with this:
    Code:
    <applet
      width="400"
      height="400"
      code="computerdetect.ComputerDetect"
      codebase="*.myurl.com"
      archive="ComputerDetect.jar">
    </applet>
    You said you manage to make you own signed cert.
    Have you also specified the security policy with the required grant access for the different system. calls?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    6
    Rep Power
    0
    Here is the error that I get when I try to run the applet using the applet tags you provided.

    java.lang.ClassNotFoundException: detectapplet.DetectApplet
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.initAppletAdapter(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


    No idea about the security policy...
    Is that done through a manifest file or a code tag?
  18. #10
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Did you change the codebase to the correct path?

    You should also be aware of below information regarding security:
    - Why are Java applications blocked by your security settings with the latest Java?
    - How do I control when an untrusted applet or application runs in my web browser?

IMN logo majestic logo threadwatch logo seochat tools logo