[Java] Opening CD drive

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

[Java] Opening CD drive

Post by anykey »

Is there an easy way to open the CD drive of a computer using java?
Not that it serves any particular purpose or anything. My plan is to create another annoying program with it (hey, I've got to keep myself entertained in compsci somehow!).
Speaking of annoying programs, I may release my ALT+TAB program, which presses alt+tab randomly (every couple of minutes or so). There's got to be somebody out that that would find it useful!
I think, therefore iMac
Image
Andy_J
Calc Master
Posts: 1110
Joined: Mon 20 Dec, 2004 10:01 pm
Location: In the state of Roo Fearing
Contact:

Post by Andy_J »

I would hazard to guess you'd have to do it platform-dependent, and assume they have a program installed that can smecks up the hardware appropriately to do it.

I can't find anything in a cursory google on Google about doing it with just Java.
ImageImage
Image
CompWiz
Calc King
Posts: 1950
Joined: Thu 13 Oct, 2005 1:54 pm
Location: UB

Post by CompWiz »

I'm pretty sure that there is a freeware program available for download that will make the cd tray go in and out(you set the interval), but I have no idea how you'd do it in java.
In Memory of the Maxcoderz Trophy Image
User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

Post by anykey »

When I used python more often, I remember the PyGame package had an eject function that essentially did this. Maybe a little more googling will turn something up.
I think, therefore iMac
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Under Windows, at least, you could use MCI control strings to do this (check the platform SDK - MCIWNDM_EJECT) but I don't know what Java's stance on what the CLI calls P/Invoke is, given Java's write-once-run-on-anything stance.
User avatar
Captain_America
New Member
Posts: 25
Joined: Wed 27 Sep, 2006 10:18 pm

Post by Captain_America »

Did you ever get around to releasing that alt tab deal? I could see that being loads of fun :lol:
User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

Post by anykey »

Not yet! I might do it later today, depending on how lazy I am.
I think, therefore iMac
Image
User avatar
elfprince13
Sir Posts-A-Lot
Posts: 234
Joined: Sun 11 Dec, 2005 2:21 am
Contact:

Post by elfprince13 »

there is no platform independent way of doing it in java. sorry. on a side note, I have some fun, mayhem causing code:

Code: Select all

import java.awt.*;
import java.util.*;
import java.awt.event.*;

class Robo{

  static int oldx = 0;
  static int oldy = 0;
  static int newx = 0;
  static int newy = 0;
  static Toolkit tk = Toolkit.getDefaultToolkit();
  static final int width = tk.getScreenSize().width;
  static final int height = tk.getScreenSize().height;
  
   public static void main(String[] args) throws Exception{
      Robot robo = new Robot();
      Random r = new Random(System.currentTimeMillis());
      for(int I = 0; I<50; I++){
	     oldx = MouseInfo.getPointerInfo().getLocation().x;
  		 oldy = MouseInfo.getPointerInfo().getLocation().y;
	     newx = r.nextInt(width);
	     newy = r.nextInt(height);
	     int xdist = Math.abs(oldx - newx);
	     int ydist = Math.abs(oldy - newy);
	     int destdist = (int)Math.sqrt(Math.pow(xdist,2)+Math.pow(ydist,2));
	     for(int i = 0; i<destdist; i++){
	         robo.mouseMove((int)(xdist/destdist),(int)(ydist/destdist) );
         }
         robo.delay(250);
         robo.mousePress(InputEvent.BUTTON1_MASK);
         robo.mouseRelease(InputEvent.BUTTON1_MASK);
      }
	robo.keyPress(KeyEvent.VK_SHIFT);
	robo.keyPress(KeyEvent.VK_CONTROL);
	robo.keyPress(KeyEvent.VK_ESCAPE);
	robo.keyRelease(KeyEvent.VK_SHIFT);
	robo.keyRelease(KeyEvent.VK_CONTROL);
	robo.keyRelease(KeyEvent.VK_ESCAPE);
   }

}
[edit]
code updated for more fun :D
Last edited by elfprince13 on Tue 19 Dec, 2006 10:46 pm, edited 1 time in total.
User avatar
Captain_America
New Member
Posts: 25
Joined: Wed 27 Sep, 2006 10:18 pm

Post by Captain_America »

I know I am probably going to look like a moron for saying this but after checking google I would like to know what do i need to compile/run java?And what do i save it as?
User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

Post by anykey »

How about I just give you a jar file that you can execute?
I couldn't find the tab one, but I did fix my screen-flipping program:
http://eric.woodtx.com/coding/annoying.jar
Enjoy!
I think, therefore iMac
Image
User avatar
Captain_America
New Member
Posts: 25
Joined: Wed 27 Sep, 2006 10:18 pm

Post by Captain_America »

Ah thank you. I intend on putting this on my brother thumb drive and just making a little autorun script so that everytime he inserts it activates
:P
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

You still need the JRE installed though (should be on Sun's website).
User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

Post by anykey »

Most people have it installed for one reason or another, so it shouldn't be too much of a problem.
I think, therefore iMac
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3087
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Post by benryves »

Not since Microsoft stopped shipping it. If you're running an old version of Windows, you might well have it, but I find it rather unlikely. No school or work machines I've ever used have had it installed.

In any case, you can download it from here.
User avatar
anykey
Extreme Poster
Posts: 420
Joined: Mon 31 Jan, 2005 3:36 am
Location: In the matrix
Contact:

Post by anykey »

School computers *never* have any stuff installed. I've found that most people have it installed because certain applications needed it, sometimes even for websites.
If only windows shipped with stuff like that...too bad it's bloated with other crap! Unix distros manage to include Java, Python, and many other runtime environments and still aren't as bloated.
I think, therefore iMac
Image
Post Reply