Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

disk volume label

Posted on 2002-04-04
6
564 Views
Last Modified: 2008-02-01
how can i read the volume label of a disk?
i want to build a cd catalog-application
it just has to run on windows
0
Comment
Question by:atombender
6 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6917882
If you want to use JDK 1.4 you don't have to ask yourself this question, itdoes automatically this for you. Try to use for example a JFileChooser from JDK1.4.

If not, I believe with native code invoked by Java (Using Runtime.getDefaultRuntime().execute("cmd") and reading from the input stream of the resulted process).
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6917885
Correction : (Using Runtime.getRuntime().execute("cmd") ....
0
 

Author Comment

by:atombender
ID: 6917951
unfortunately i have jdk1.3
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Expert Comment

by:rashidkamranbs
ID: 6919037
import java.io.*;

public class Test{
     public static void main(String args[])throws Exception{
          Runtime rt=Runtime.getRuntime();
          Process p=rt.exec("chkdsk f:");
          BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
          System.out.println(in.readLine());
          System.out.println(in.readLine());
          p.destroy();
         
     }
}
OUT PUT IS
The type of the file system is FAT32.
Volume PROGRAMS created 10/22/2001 9:39 AM

Its a bit funny .. but belive me reads the Volume label and type of drive F, there on Windows XP with JRE 1.3.. if ya have ne other progam that could write the Disk Volume you can use instead.. as i used chkdsk.. no internal command i think

points as it works :-) kiddin
0
 
LVL 3

Accepted Solution

by:
msterjev earned 100 total points
ID: 6919274
OK. Some JNI looks like this:

Kernel32.java


import java.util.*;

public class Kernel32
{
     static
     {
          System.loadLibrary("JKernel32");
     }
     
     private static native String getLogicalDriveStrings();
     public static native String getVolumeLabel(String root);
     
     public static Enumeration enumLogicalDrives()
     {
          Vector v=new Vector();
          String drives=getLogicalDriveStrings();
          StringTokenizer st=new StringTokenizer(drives);
          while(st.hasMoreTokens())
               v.addElement(st.nextToken());
          return v.elements();
     }
}


Generate header with javah:

javah Kernel32

Start Visual C++ and make new dll with the name JKernel32.
Import here previously generate Kernel32.h.

Insert new C++ file and type here:

#include "Kernel32.h"
#include <windows.h>

JNIEXPORT jstring JNICALL Java_Kernel32_getLogicalDriveStrings(JNIEnv * env, jclass c)
{
     char buffer[256]={0};
     char drives[256]={0};
     ::GetLogicalDriveStrings(256,buffer);
     char *p=buffer;
     lstrcpy(drives,"");
     while(*p!=0)
     {
          strncat(drives,p,3);
          strcat(drives," ");
          p+=4;
     }
     return
          env->NewStringUTF(drives);
}


JNIEXPORT jstring JNICALL Java_Kernel32_getVolumeLabel(JNIEnv * env, jclass c, jstring r)
{
     const char *root=env->GetStringUTFChars(r,0);
     char volumeNameBuffer[MAX_PATH];
    DWORD lVolumeSerialNumber=0;
     DWORD lMaximumComponentLength=0;
     DWORD lFileSystemFlags=0;
     char fileSystemNameBuffer[10];
     if(::GetVolumeInformation(root,volumeNameBuffer,MAX_PATH,&lVolumeSerialNumber,&lMaximumComponentLength,&lFileSystemFlags,fileSystemNameBuffer,10))
     {
          env->ReleaseStringUTFChars(r,root);
          return env->NewStringUTF(volumeNameBuffer);
     }
     env->ReleaseStringUTFChars(r,root);
     return NULL;
}


Compile the project and put  JKernel32.dll into the same directory as Kernel32 class.

Try this:

Test.java

import java.util.*;

public class Test
{
     public static void main(String[] args)
     {
          String drive,label;
          Enumeration e=Kernel32.enumLogicalDrives();
          while(e.hasMoreElements())
          {
               drive=(String)e.nextElement();
               label=Kernel32.getVolumeLabel(drive);
               System.out.println(drive+":"+label);
          }
     }
}

Enjoy.

P.S. JNI is such a cool thing. You can extend Java with almost everything you want!!!!
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6920402
Some comment.....
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 62
Maven Project: Hibernate Dependencies Conflict 10 37
How do I remove an object from a 3 40
American Express @Work site and Java 4 10
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question