Finding the average CPU usage for Windows machine in Java [duplicate] - java

This question already has answers here:
How do I monitor the computer's CPU, memory, and disk usage in Java?
(12 answers)
Closed 9 years ago.
I have searched all over the internet for about two days now. I have found a lot about how to find the CPU usage using VB and anything in the .NET framework. I am not using the .NET framework so these are irrelevant, I feel.
What I really need is results similar to the ones shown in the "CPU Usage" section in a Windows Task Manager under the "Performance" tab. If I could find a way to mimic that calculation, then that is exactly what I am looking for. I need the percentage of CPU Usage.
Obviously, Java is not the ideal language for finding the CPU usage of a computer, but that is the language I am using and I have been able to get everything else I need about the computer except this.
I am using Windows 7 with Java version 7 update 21. Eclipse is my IDE.
This is my first time posting a question on here, so if I have left anything out or something is not clear, please ask and I will do my best to explain more of what I mean.

There are a couple of ways to do this; one is to run a command-line tool and parse the output. On Windows 7, you can run
c:\Windows\system32\typeperf "\processor(_total)\% processor time"
Try it -- it prints a few lines of sampled CPU load data.
Another way is to use OperatingSystemMXBean .

Related

Eject USB drive from Java [duplicate]

This question already has answers here:
How Do I Eject a Volume in Java?
(2 answers)
Closed 3 years ago.
I know that questions like this have been asked before, but I couldn't find any more recent ones and I have a twist to my question.
I've developed an application in Java that is designed to run on removable media, and work on both Windows and Mac. I would like to add a button to safely remove/eject the device, if it is supported (i.e a USB drive). Is there a command line for each operating system that would allow me to do this?
I know that it can be done by an application running on the device to be ejected, because I've seen one that does it, but obviously I understand there are certain limitations to Java.
Thanks in advance
This is something that you will have to do by invoking an auxiliary application. These applications are not platform independent as you wish. So, to do that, find out which OS you are on by using System.getProperty("os.name") and invoke the appropriate command for the detected OS. Invoking applications is done with Runtime.getRuntime().exec(). Search for the commands you need for each OS.

How to collect JVM performance statistics from java [duplicate]

This question already has answers here:
Can jconsole data be retrieved from the command line?
(9 answers)
Closed 7 years ago.
I want to write a JUnit testcase that will also be able to monitor and collect the parameters of a JVM, like the memory usage, heap size and other custom JMX managed beans etc. I will be having the JMX address of the JVM. More clearly, I would like to know if I could gather the information that jconsole provides but programatically without a UI. And then I would want to check if the JVM performance has degraded or not by integrating it with Jenkins. Are there any already available tools that do this?
You might consider using Virtual Machine Agent, here is a good educational article
Also you can use Profiler tools in eclipse to monitor your program performance

How to write a program in Java (Eclipse) to find the speed of the processor [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Detect CPU Speed/Memory/Internet Speed using Java?
I am a beginner and I want to learn how to write Java programs for Android apps. Thinking that it would not be too difficult I thought of writing a benchmark utility.
I have spent two days solid looking through a host of tutorials and I'm no nearer my goal, in fact I think that I'm more confused. The more I read the more I struggle to understand. Maybe I should take up knitting?
How do I ask the processor what speed it's running at?
If I write code (currentTimeMillis or nanoTime) how do I know that the processor is only doing what I've asked it to do?
How do I use bogoMIPS? To have access to it's information or implement it into my code.
I've read /proc/cpuinfo but how do I get it's source code to implement into my own code?
Or, how do I implement it into my code (but I don't know how it works, so I don't really know what the results mean.
I guess that's the bottom line question: how do I write the basic code to get the processors speed (in GB's), so that what it gives me is exactly what I am asking it. How do I control the processor so that it does what I want (in terms of calculating the speed)?
Do I simply say "processor do this calculation and nothing else, so that I know how fast you're running"?
I've followed various tutorials on Java over the past few weeks, but I'm still blank on this.
An interesting question, but without wanting to be discouraging this could be one of the most difficult things you could start with! The closer you get to the hardware, the more challenging the code is going to be. Try a simpler app which is all about UI design, and with which you can learn the basics of UI "widgets" like buttons, radion buttons, checkboxes, lists, etc, and of data storage/retrieval.
The old cliche of a "to-do list" app is a good idea to start with, if it's really learning the platform you're interested in. Just don't expect to sell millions of the finished app!

Has Java ever been used in outer space? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
In response to a question about examples of Java usages, I bumped across some articles where NASA used Java for ground control in a mission to Mars but I couldn't find out if it has ever been used outside of Earth. Do you know of any such instances?
Here you will find a paper discussing the current state of using Java in space applications:
REAL-TIME JAVA IN SPACE: POTENTIAL BENEFITS AND OPEN CHALLENGES
They say:
In short time, it is unlikely that
with current Java implementations the
whole spacecraft on-board software
could be written in Java. However,
Java could be used as an isolation
platform for software that has not
been assigned the highest criticality,
while the critical code would still be
written in Ada or C.
Here one of the same authors reports on a successful test flight of an unmanned aerial vehicle (UAV) that uses Ovm (open source RTSJ (Real Time Specification for Java) implementation):
A Real-time Java Virtual Machine for Avionics
One fact often overlooked is that most space probes use older processors. They simply cannot run current commercial software. They are not fast enough or powerful enough. IC chips in space are exposed to high levels of cosmic rays. They energetic particles can destroy the small nanometer components on modern CPUs and other IC chips. To work reliably in space you either need specially designed ICs with redundant components, spend a lot on heavy shielding, or use older IC chips which have larger components that can take a hit from a cosmic ray without being destroyed.
Most of these older CPUs will run with a RTOS such as VxWorks and are either programed in assembler or using a language like C and still get decent performance.
Probably on the documentation laptops they take up there on the Shuttle, at the very least.
Java is also mentioned here, which implies that there is at least Java code that can talk to some spacecraft, if not actually run on them: http://www.dtnrg.org/wiki/Code
According to this article, the Ground Operations Center uses it for 3D Mapping and Planning. A comment on this forum claims the Mars Lander runs VxWorks.
Edit: Confirmed by Wind River, the Spirit and Opportunity run VxWorks RTOS. No other references where Java has been explicitly used in Space.
I think I read a few years ago that some probe or satellite used Java as part of their analyzation equipment but I doubt that mission critical software will be developed in Java today.
However: With all the applications used by astronauts on their Laptops I wouldn't be surprised if there were applications written in Java. Maybe some locally installed webapp that is used as a bugtracker.
I cannot speak with any authority with respect to the software onboard spacecraft, but I can say definitively that Java is used by NASA (or its affiliates) to handle the data from the various missions. I recently worked at a laboratory for space physics, which handled data and telemetry for in-orbit spacecraft, and the development team (of which I was a part) worked almost exclusively in Java.

What are the main benefits of using Mono over Java? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What are the main benefits of using Mono over Java (or any other "free" or Linux-friendly language/runtime)?
Mono will always trail behind the latest developments in the .NET framework, why would you want to use Mono over another traditional open-source language/framework (Java)?
EDIT:
Like someone mentioned below, I guess the root question is, why would you want to use .NET on the Linux platform?
The answer is pretty obvious: because you want to use .Net on Linux.
This of course begs the question (which I think is really what you're getting at): why would you want to use .Net on Linux (over Java)?
Lots of reasons:
Common code between your server and, say, a WPF or Winforms app;
Use of a particular .Net language, like F#;
Language features that aren't in Java (closures, operator overloading, partial classes, runtime generics, indexers, delegates, LINQ, var types, etc etc etc);
Your skills or those of your team are already in C#;
etc.
Personally, i have more trust in Linux platform as server, but want to use C# as program language.
You want to use C# on the iPhone, Wii, and possibly Android at some future point. See this link.
http://arstechnica.com/open-source/news/2009/01/open-source-mono-framework-brings-c-to-iphone-and-wii.ars
Some people like C# better than Java. Also, consider that C# is an ISO standard language while Java is not. Maybe you want to run ASP.NET code on a Linux server? There's lots of reasons, really.
I think it comes down to personal preference. I am comfortable with c#, and I want to develop apps that I can run on my Ubuntu Laptop or my Vista Laptop. Therefore, I use mono. If I were more comfortable with Java, then that's probably what I'd use.
Just my 2 cents.
I worked for a company once whose main product was written in ASP.NET. They realized that they could probably increase their market share if they could make their product run on Linux servers. This would of course be quite difficult if not for Mono. I was given the task of spending some time seeing how much (if any) of their code would run under Mono. This would have been about 2 years ago - most of it compiled, but Mono's support for generics at the time left a little to be desired. I think its matured since then, but some of the code wouldn't even parse. The code also made use of p/invoke a bit to call native code, so that made things a little bit interesting as well. It certainly was fun trying to get it running, though.
Since Microsoft submits changes to the C# ISO standard before they make them, in theory Mono doesn't NEED to trail .NET. In reality, the big old MS budget gets to the finish line first.
Mono is behind on some things, but way ahead on others. For example, Mono supports SSE and full ahead of time compilation - built in. On the other hand... it's going to take Mono sometime to implement all the new upcoming stuff in the upcoming .net release... for example... the quite extensive parallel libraries.
As everybody says, "to run C# on Linux" or "to be cross-platform" or whatever.
The reason why I am interested in Mono -- and I haven't been convinced to touch it, yet -- is the possibility to take advantage of all the things that .Net has to offer that Java does not. Basically your question does come down to a Java vs. .Net comparison, and I'm not going to start running through a million points of comparison between them (in Google you'll find scores of comparisons of J2EE vs. ASP.NET, which is one part, and Java vs. C#, which is another).
But what is the big difference between .Net and Java? It has to do with the monolithic force behind .Net (Microsoft, and Novell is trying to translate that to other platforms) or the multi-vendor approach of Java. Just the process of choosing an App Server might be enough for some people to choose Mono.
What does one big monolithic vendor like Microsoft give you that Java seems to not have:
Centralized documentation (was "Amazingly good documentation, all in one place" but several people have told me I'm wrong)
No need to make individual platform choices (e.g., ASP.NET or ASP.NET, not servlets+JSP+JSTL?+Axis)
Other stuff I am forgetting
And then there are the other massive differences: Java is a single language and Mono (I think) can run CLR-compiled code from a variety of languages.
in one word? Cross-platform
Anyone who uses Mono faces software patent danger from Microsoft.
That's why not only Mono should be avoided for new applications but also C# in first place.
Migrating to Mono only make sense if you already stuck with Windows and want to escape.
Why free software shouldn't depend on Mono or C#
How come noone has mentioned that Linux is free?
I have used mono as a cross-compiler, in effect. I added some "exec" lines to an existing java/ant build file to call mono as the compiler, so there was a single, complete, command line build process.
I had to write a C# wrapper layer around some PC code which our java-on-linux code base had to use, but I couldn't bring myself to require starting up Visual Studio (on a seperate computer) just to do the check-out and build process.

Categories

Resources