0

I have a thread that, when the current application closes, must start the main() method of another class.
I included ClassName.main(someStringArray) in the run() of the thread but the method wasn't called. What might have gone wrong?
The Thread I defined:

private class VideoCreator extends Thread{ public VideoCreator(){ pathToPass = savePath + "/" + "video.mov"; passVect.add("-w"); passVect.add("1280"); passVect.add("-h"); passVect.add("800"); passVect.add("-f"); passVect.add("25"); passVect.add("-o"); passVect.add(pathToPass); } @Override public void run(){ try{ jpegFiles = Files.newDirectoryStream(Paths.get(pathToPass).getParent(),"*.jpg"); for(Path jpegFile : jpegFiles){ passVect.add(jpegFile.toString()); } }catch(IOException e){ } try{ JpegImagesToMovie.main((String[])passVect.toArray()); }catch(Exception e){ System.out.println("Dammit Error!"); e.printStackTrace(); } } public void cleanUp(){ } String pathToPass; Vector<String> passVect = new Vector<>(100,200); DirectoryStream<Path> jpegFiles; } 
8
  • Was there some kind of exception? Commented Sep 13, 2012 at 18:19
  • 1
    stackoverflow.com/questions/5989145/… Commented Sep 13, 2012 at 18:19
  • @MvG Class cast Exception. I tried to convert an array returned from java.util.Vector class' toArray to a String[]. The Vector is Vector<String> Commented Sep 13, 2012 at 18:22
  • @ErhanBagdemir, the referenced question is about starting the other application in a separate process, not a new thread of the same process. Commented Sep 13, 2012 at 18:23
  • 1
    @MvG I just posted the code so see it as it might help :) Commented Sep 13, 2012 at 18:27

1 Answer 1

1

Instead of

(String[])passVect.toArray() 

you should write

passVect.toArray(new String[passVect.size()]) 

or (shorter but less performant)

passVect.toArray(new String[0]) 

The reason is that toArray() will always return an Object[] array, which you cannot convert to a String[] array even if all its members are strings. (The reverse, by the way, is possible: you can pass a String[] in places expecting Object[], which is used by various methods of the Arrays class. In fact, the thing returned from the toArray() method might have been a String[], even if in a standards-compliant implementation it is not. This is the reason why the compiler didn't complain: it doesn't know or care about the internals of the method, and judging from the return type, an explicit cast to an array of a subclass might be possible if the array was created as such.)

The toArray(T[]) call returns an array of the required type, if you pass an array of that type as an argument. If the passed argument has the correct length, it will be used directly; otherwise a new array will be allocated. For this reason, allocating the correct length in the first place will avoid one allocation along the way.

Sign up to request clarification or add additional context in comments.

5 Comments

Wait, I guess it is the Path being added that is causing the problem. Can you rectify that? The toString() for Path returns nothin as I tried printing it using System.out.println() and got nothing on the screen
Sir, I believe it is the DirectoryStream<> causing the problem.
@DummyDerp: The above fix should avoid the class cast exception encountered when turning the Vector into an array. There might be other errors before or after that, and you might learn more by not ignoring exceptions the way your first catch block does. But as these appear to be independent issues, I suggest you have a look at the next exception, try to fix that yourself, and ask a new and detailed question including code and exception if that fails.
The error was because passPath was getting a null value assigned to it. I fixed it. Will let you know the outcome in a few minutes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.