1

to be brief - I have written a small Parser.class in Java and to it a small batch script that executes the command: java Parser "%1" The idea is that I want to drag-drop logfiles onto the batch script, which would then be handed over to my class as arguments. So far everything works fine if I put the logfile in the same folder as the batch script and java class. However, if the logfile is e.g. on the Desktop and I drag it directly onto the batch file in another folder, I receive the error that the main class Parser couldn't be found or loaded. Any idea what might be wrong here? I want to make it possible to drag the logfiles from any location onto the batch.

Edit: This is my batch-script:

@echo off&&setLocal EnableDelayedExpansion 
if "%1"=="" ( 
echo Please drag your file onto the icon. 
echo. 
pause&&goto :eof) 
cls 
java Parser "%~1" 

Any idea what to do now? The problem with the spaces in the filepath should be avoided using "~", and as I said my batch script works as expected when all files are in the same folder...

Kara
  • 6,115
  • 16
  • 50
  • 57

3 Answers3

1

It sounds like the batch file is assuming that the default classpath of "the current working directory" is good enough - but it won't be if the current working directory is some arbitrary folder.

If this is just for convenience and the class files won't move, you could hard code the classpath:

java -cp c:\foo\bar Parser "%1"

(where c:\foo\bar is the directory containing the class files). For a more robust approach, you should make it detect where the batch file is, but I don't know how to do that offhand...

Once you've fixed the problem of the class not being found you may also run into the quoting problems mentioned in other comments and answers - but until you can get your code running, nothing else is relevant :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

I don't see your bat file contents, but i suppose you made one of usual bat-file beginner's mistakes. For example you could forgot to surround paths, containing spaces with double quotes.In your case this will look like this:

java MyClass "%1"

or

java -cp MyClass "%1"

or

java -jar myJar.jar "%1"

Or you should do

**'cd %~dp0'**

in the begining of your script to let script know tnat java class is in current directory.

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72
  • Hello, thanks for your reply. This is my batch-script: @echo off&&setLocal EnableDelayedExpansion if "%1"=="" ( echo Please drag your file onto the icon. echo. pause&&goto :eof) cls java Parser "%~1" pause Any idea what to do now? – Adnan Pasic Mar 16 '12 at 08:58
0

Do you tried to display the content of %1?
Then you will see that windows sometimes will enclose it with quotes.

So your command will expand to something like

java Parser ""C:\documents & setting\mytest.log""

To solve this you could simple use the tilde syntax (removing enclosing quotes)

java Parser "%~1"

But there is another problem, sometimes windows doesn't enclose critical content with quotes.

Like Cool&Stuff.mp3
This will create strange errors, and it can't be solved with using %1 or %~1, then you need to use the %cmdcmdline% variable, described at Drag and drop batch file for multiple files

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225