> Hi,
>
> How can i get the physical and logical CPU count?
>
> please help. i could not found the solution for this
> problem in pure java.
> there is an API for the intel processors provided by
> intel, but its in C. How can i do the same from java?
You can't. What do you think would happen if you tried to use that API on another processor?
>What do you think would happen if you tried to use that API on another processor?
you are right, Intel API in C won't work on processors other than Intel's.
But if i could do it only for Intel's that's fine for me.
i like to keep my program in pure java but is there any way i can call C api from java and get the Physical CPU count from there?
i think only C can go to this level. as i remember, i had to parse the whole 'ipconfig' command's response to get the network information (i had 2 network cards on my system).
Ok, this solves the problem in a way ( i guess its the only way).
another question: is there any way to do #ifdefine and #endIf in java.
i mean the conditional compilation like in C/C++.
i am stuck to create four files for a small change in code. i don't want it to be visible to the user in any way so i can't read it from a file external to my jar file.
You can do:private final boolean SOMEVALUE = true;
...
if (SOMEVALUE) {
// conditional code here
}
Because it's final, the compiler will remove the code entirely if SOMEVALUE is false.
Or, you can just use the C preprocessor if you want.
Or find another solution. Maybe just put this data in a jar. You do realize that users can always decompile your code, right?
You get a lot of information from java.net.NetworkInterface. Why did you need to parse ipconfig?
It sounds like you're doing a lot of things that Java was never meant to do.
Message was edited by:
paulcw
> i am stuck to create four files for a small change in
> code. i don't want it to be visible to the user in
> any way so i can't read it from a file external to my
> jar file.
Are you talking about the JNI library (DLL) that you need to call?
One solution to this is to store the DLL as a resource in your jarfile. Extract the resource to a temporary file on disk (unfortunately, this step is needed), then use the System.loadLibrary() method (or whatever it's called) that takes a full pathname to the library.
Not sure why you want the #ifdef, is it to see if the user is running on a version that includes the CPU check? If that's the case, then it's a good use for reflection: try to load the class with Class.forName(); if that fails, you don't have your native classes available.
>Because it's final, the compiler will remove the code entirely if SOMEVALUE is false.
thanks, i got the idea. i can use the final boolean variable.
>You get a lot of information from java.net.NetworkInterface. Why did you need to parse ipconfig?
i wanted the mac address.
>It sounds like you're doing a lot of things that Java was never meant to do.
i'm new to java. and you guys are helping alot. Thanks.
Basically its like access to some features based on license type.
I want a user with one license type to use some features and not others.
and there are four such possibilities.
e.g. think of it as a client application connecting to a remote server. based on a certain license the user can do remote calls, and stuff like that. and for another license type he can't.
Personally, I'd try to do something like this not with conditional compilation, but with conditional deployment. Design your app so some classes can be missing, or be replaced with dummy classes. Then in the jars you send to the user, include the classes, or don't, depending on their license.
It seems to me that this would be easier to manage than code that changes per user. Also, I suspect, easier to test. And you don't need to worry about clues appearing in decompiled code.
It will, but if you're distributing different versions to different people I think it would be easier to mix-and-match class files that have a single compiled version, than to manage different compiled versions. You could for example make an Ant script that compiles everything and then generates four different jar files, one for each type of customer. Also if you have single compiled versions of each class, then (for example) you can tell more easily from a stack trace what's going on -- there's only that one version of any class in the stack trace. If you had different versions of the compiled classes out there, you wouldn't know what version the user had.
It'll work with the final boolean, but I think it would be easier from a logistical perspective to just distribute different classes (with unique names, etc.).
I don't know what you mean about "the one project". Most uses of "project" in IDEs and the like (which I've seen anyway) allow for multiple classes, plus build scripts, etc.
>'ll work with the final boolean, but I think it would be easier from a logistical perspective to just distribute different classes (with unique names, etc.).
different classes with unique name is not good. it creates confusion for the end user. o let me explain again: i'm giving some API to the user, so unique names is not good.
Ant scripts idea seems better.
i'm using the "sun java studio enterprise 8.1" it uses ANT scripts.
now i'm looking for ANT scripts.
> different classes with unique name is not good. it
> creates confusion for the end user. o let me explain
> again: i'm giving some API to the user, so unique
> names is not good.
This is where I'd use an interface to define the API, then provide different implementations of it: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
Absolutely.
There are many cases in the existing API where you, the user of the API, are using particular unnamed classes without knowing it, because you're dealing with the interface.
So for example, there might be a class, say, PlanetaryCalculatorFactory, and an abstract class or interface named PlanetaryCalculator. The user would do something like:
PlanetaryCalculator pc = PlanetaryCalculatorFactory.getCalculator("Jupiter");
pc.findOrbit(someProbe);
And it would turn out that someProbe is actually an instance of the class GasGiantCalculator. But this is never mentioned, because it doesn't have to be. And if for some reason the user only bought the license that gives them access to Mercury and Venus, then you might send them an UpgradeYourLicense object, which implements PlanetaryCalculator but returns 0 or null for everything.
OK maybe that's not the best example but hopefully you get the idea.
My guess is that it wouldn't effect performance at all; it might improve performance because it would allow you to have different implementations tuned for whatever it is they're doing under whatever conditions it's operating under.
But I really don't know what you're implementing so I don't know. Profile profile profile.
> i got your point. but what will be its impact on the
> performance of my API?
That's where profiling comes in. Implement, profile, and then figure out how to resolve any hotspots.
> The sole purpose of my application is performance.
Then why aren't you writing in assembly language?
And really, the sole purpose is to provide useful functionality to some end-user, right? Performance is a part of delivering that functionality, but it's hardly the sole purpose. If it were, you could just write a 3-line program that starts, does nothing, and exits ... can't beat that for performance.