?
Solved

disk volume label

Posted on 2002-04-04
6
Medium Priority
?
588 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
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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