Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm having a little trouble with parsing strings in batch. Basically, I'm trying to make a batch script to automatically compile and run Java files, and make that script the default program for .java files. The compiling part works fine, but the running part is harder. The JRE takes as an argument just the filename, without an extension (eg for a file named Test.java with bytecode Test.class, one would type "java Test"). My batch code so far looks like this:
"C:\Program Files\Java\jdk1.5.0_12\bin\javac.exe" %1
set raw=%1
set no_period=%raw:.= %
set no_quotes=%no_period:"=%
call cr_parse %no_quotes%
"C:\Program Files\Java\jre1.6.0_05\bin\java.exe" %first_word%This takes the argument, which is the full file path in quotes, and compiles it. Then it strips off the quotes and replaces the period in the file name with a space. The called file cr_parse simply the first argument passed to it in the variable %first_word%, in this case the file path and file name, but not the extension. For example, this input:
"C:\Documents\Test.java"
would give this to the JRE:
C:\Documents\Test
with no quotes. The problem is, I need to just give it the file name, in this case "Test". How could I do this?

For me, this is just giving me the full path without the extension. I can already get this; is there a way to get just the filename, without the path?

That one does basically the same thing, but without the drive letter. For example, it might give:
\Documents\Test
How can I get just the filename, in this case:
Test

"That one does basically the same thing"
Not obvious how. Better check your typing.
=====================================
If at first you don't succeed, you're about average.M2

If you just want to compile and run the java program from a single batch file, why don't you try this one?
Hope you have already set the java bin path and class path in My Computer>Properties>Advanced>Environment Variables.
If not then you can add it in the batch file. of course it is as simple as...
set path = %path%;<path of java bin directory>
set classpath = %classpath%;.;<path of jre lib directory>rem lets call this batch file as execute.bat
javac %1.java
java %1Obviously you have to supply only the name (without the .java extension) as the argument of the batch file.
e.g.
execute testWould you like to make this batch file more compact i.e. containing less than 2 lines. ;)

That wouldn't really work for my purpose; what I need is a batch which I can set as the default program to open a .java file with - ie, if you double click on a .java file, that's what is run. Therefore, what will be passed to it is the full name and path of the file. This is the code which I have been using, and it works well:
@echo off
echo Compiling...
echo.
"C:\Program Files\Java\jdk1.6.0_06\bin\javac.exe" %1
echo.
echo Done!
echo Running...
echo.
echo Program output:
echo.
"C:\Program Files\Java\jre1.6.0_06\bin\java.exe" %~n1
echo.
echo Execution complete.
pause

![]() |
Batch file copy assistanc...
|
How to read the 3rd line ...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |