Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

disk volume label

Posted on 2002-04-04
6
Medium Priority
?
595 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

Independent Software Vendors: 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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

609 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