Hi all, I'm kind of new in Java MIDlet, and i'm having some troubles.
Let say i got a MIDlet project, named APP.java, and the MIDlet project runs smooth without any problem.
And i have another java application, named testing.java, and the java code runs smooth as well.
But when i try to combine testing.java inside MIDlet project, lots of errors shown.
Is it anything to deal with the package? Because my MIDlet project does not have a package.
I'm still working on in, any help would be appreciated! :)

Regards,
Keo

Hi keofua and welcome to DaniWeb :)

Can you post the error messages that you are getting please? That might help us to identify the problem.

Regards,
d

Here are the error messages.

D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:8: cannot find symbol
symbol : class KeyPair
location: package java.security
import java.security.KeyPair;
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:9: cannot find symbol
symbol : class KeyPairGenerator
location: package java.security
import java.security.KeyPairGenerator;
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:11: cannot find symbol
symbol : class PrivateKey
location: package java.security
import java.security.PrivateKey;
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:12: cannot find symbol
symbol : class SecureRandom
location: package java.security
import java.security.SecureRandom;
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:13: cannot find symbol
symbol : class Security
location: package java.security
import java.security.Security;
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:22: cannot find symbol
symbol : class KeyPair
location: class pki.Generator
public KeyPair generateKeyPair(long seed)throws Exception {
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:23: cannot access java.security.Provider
class file for java.security.Provider not found
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:23: cannot find symbol
symbol : variable Security
location: class pki.Generator
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:24: cannot find symbol
symbol : class KeyPairGenerator
location: class pki.Generator
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:24: cannot find symbol
symbol : variable KeyPairGenerator
location: class pki.Generator
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:25: cannot find symbol
symbol : class SecureRandom
location: class pki.Generator
SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:25: cannot find symbol
symbol : variable SecureRandom
location: class pki.Generator
SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:32: cannot find symbol
symbol : class Main
location: class pki.Generator
Main kpge = new Main();
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:32: cannot find symbol
symbol : class Main
location: class pki.Generator
Main kpge = new Main();
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:33: cannot find symbol
symbol : class KeyPair
location: class pki.Generator
KeyPair kp = kpge.generateKeyPair(999);
D:\My Documents\NetBeansProjects\PKI\src\PKI\Generator.java:42: cannot find symbol
symbol : class PrivateKey
location: class pki.Generator
PrivateKey priKey = kp.getPrivate();
16 errors

Compress your NetBeans project into ZIP file add at it as attachment to your next post. I will try to have look at it as soon as possible. But from above messages it seems you are missing some declarations of various types. Look like you did not merge these two projects properly

This are my codes. The whole project is too big to be uploaded.
SIPDemo.java

Generator.java

My apologies for late reply, but I had some problems with my pc which need it to be sorted before attending to your problem.

Your problems lays with use of packages that are not supported by Microedition. As you can see from JME API there is nothing such as java.security package as we know it from Java SE API. Therefore different approach is need it.

As I do not know full scope of your project I can only point you in general direction. Understanding MIDP 2.0's Security Architecture article by by Jonathan Knudsen would be best place to start.

Thanks for the advise and sorry for late replying. I've already solve out the errors, it's the library problem.

Allow me to ask one more question. Lets say i have got one file that act as a random number generation, "Generator.java".
In a MIDlet file, "PKI.java", i want to take the number from "Generator.java" and use in "PKI.java", is it possible? How to do it?

Just provide public getter method as in any other Java coding and you are sorted, or create method that will have some return type if you need to pass arguments to generator

I see. Can you give some brief step by step coding or some example?
I can do it in C++, but in Java, is a bit difficult for me.
Thanks.

In Generator.java

public int generateNum(){
    int number;
    //your process of generating random number
    return number;
}
//OR with argument
public int generateNum(int numArgs){
    int number;
    //your process of generating random number with use of numArgs
    return number;
}

in PKI.java

// somewhere in the coding
Generator generator = new Generator();
int randomNum = generator.generateNum();
//OR
int randomNum = generator.generateNum(10);    // generate number between 0 and 10

Thanks for the guidance, i think i starting to have some idea on it.

So, for my case, this is the generator.java

public static void main(String[] args) throws Exception {
Generator kpge = new Generator();
KeyPair kp = kpge.generateKeyPair(999);
PublicKey pubKey = kp.getPublic();
System.out.println("   Algorithm=" + pubKey.getAlgorithm());
System.out.println("   Encoded=" + pubKey.getEncoded());
System.out.println("   Format=" + pubKey.getFormat());
}

Can I get the value of pubKey.getEncoded() inside the PKI.java?
How?

I have to tell you, yo are confusing me with jumping between MIDlet and traditional Java.
Can you again explain in detail what you doing as I do not see possible connection between MIDlet and console/GUI based application. You can have MIDlet to HTTP, Web Service or basic networking, but I never seen direct MIDlet to desktop application

Sorry for confusing. My concept is as below.

The PKI.java, is the MIDlet, which held the mobile simulator and perform SIP, which involve sending message from one simulator to another.

The Generator.java, is a java file that generate a public and private key for the simulator, the simulator then will receive the keys and do encryption and digital signature and certificate.

Is it possible to do so?

Your generator class should be ordinary class not the one with main() method in. Just simple calls with few methods that are called do some precessing and provide a return, something like

public class Generator{

    public Generator(){}

    public PublicKey getPublicKey(){
        KeyPair kp = kpge.generateKeyPair(999);
        PublicKey pubKey = kp.getPublic();
        System.out.println("   Algorithm=" + pubKey.getAlgorithm());
        System.out.println("   Encoded=" + pubKey.getEncoded());
        System.out.println("   Format=" + pubKey.getFormat());
        return pubKey;
    }
}

I did not included any import or try/catch statements, that is something you should be able to add.

PS: Are you working on something like this Nokia Digital signatures - PKIMIDLET example?

I see. So now i'm doing something like below:

public class Main {
    public KeyPair generateKeyPair(long seed)throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
    SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
    rng.setSeed(seed);
    keyGenerator.initialize(1024, rng);

    return (keyGenerator.generateKeyPair());
    }
    
    public PublicKey getPublicKey() {
    
        Main kpge = new Main();
        KeyPair kp = .generateKeyPair(999);
        PublicKey pubKey = kp.getPublic();
        System.out.println("-- Public Key ----");
        System.out.println("   Algorithm=" + pubKey.getAlgorithm());
        System.out.println("   Encoded=" + pubKey.getEncoded());
        System.out.println("   Format=" + pubKey.getFormat());
        return pubKey;

    }

but i'm getting an error on line 15, a java.lang.Exception error message, and i try/catch it but it was useless. I know i did something wrong. Please correct me.

You forgot to give it an instance to which the method should be called

Main kpge = new Main();
        KeyPair kp = kpge.generateKeyPair(999);
        PublicKey pubKey = kp.getPublic();

However with your current code you just complicating your life. Do it like this

public class Generator {
    
    public PublicKey getPublicKey() throws Exception{
    
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA");
        SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN");
        rng.setSeed(seed);
        keyGenerator.initialize(1024, rng);
        KeyPair kp = keyGenerator.generateKeyPair()
        PublicKey pubKey = kp.getPublic();
        System.out.println("-- Public Key ----");
        System.out.println("   Algorithm=" + pubKey.getAlgorithm());
        System.out.println("   Encoded=" + pubKey.getEncoded());
        System.out.println("   Format=" + pubKey.getFormat());
        return pubKey;
    }
}

When you call Generator method getPublicKey() make sure you do it inside try/catch statement and take care of any exceptions

PS: Please in mind I cannot guaranty that above code will work 100% as I just simplified code provided by you

commented: Another very helpful thread by you :) +4

Thanks again. The code works.

Can i generate keypair inside MIDlet?
Or i need to have another java file to generate the keypairs then pass to the MIDlet?

You can do it in MIDlet, but I will say keep it in another class as that way your code would be easier to manage. Scrolling down hundred of lines is unpleasant sport

commented: Helpful posts in this thread! +19