Solved

disk volume label

Posted on 2002-04-04
6
535 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
Comment Utility
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
Comment Utility
Correction : (Using Runtime.getRuntime().execute("cmd") ....
0
 

Author Comment

by:atombender
Comment Utility
unfortunately i have jdk1.3
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Expert Comment

by:rashidkamranbs
Comment Utility
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
Comment Utility
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
Comment Utility
Some comment.....
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

10 Experts available now in Live!

Get 1:1 Help Now