Solved

disk volume label

Posted on 2002-04-04
6
548 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 32
unix in java example 9 52
Checkbox and ListView in Android Layout 4 56
servlet example issue 6 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now