0

I want to run a compiled .java/.class file that is located in a subfolder from the folder I am currently working in. I ran the command:

locate -br ^TheFileIWantToRun.java

so I know that the file is in the filepath:

/home/Me/Desktop/Folder1/Folder2/TheFileIWantToRun.java

I need to run this file from my current working directory:

/home/Me/Desktop/Folder1/

And I need to pass it a text file called test.

I have tried:

java -cp /home/Me/Desktop/Folder1/Folder2 TheFileIWantToRun test

But I get the error:

could not find or load the main class TheFileIWantToRun

Typically if the .java/.class file were in the same folder I would use:

java TheFileIWantToRun test

and this works.

How do I accomplish running a file from a different folder than the one I am in? Thank you.

5
  • have you checked that your folder-structure represents your package structure defined in the .java file? Commented Nov 9, 2015 at 13:19
  • @ParkerHalo, I am not sure what you mean Commented Nov 9, 2015 at 13:21
  • @ParkerHalo no there is no package information Commented Nov 9, 2015 at 13:24
  • why not just cd into that directory, call the program there and give it the full path to the text file ? Commented Nov 9, 2015 at 13:38
  • @Marged the reason is that I am creating the test text file while in the current directory and I am trying to avoid going back and forth between directories. Commented Nov 9, 2015 at 20:52

1 Answer 1

1

I suggest you try the following :

java -cp /home/Me/Desktop/Folder1/Folder2 TheFileIWantToRun test

If test is not in the current directory then you should provide the full file path.

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

4 Comments

When I try the above it says could not find or load main class .home.Me.Desktop.Folder1.Folder2.TheFileIWantToRun
I have just edited my answer, I tried it my self and it worked for me, it's like the one you have already posted with your question. Are you using any package naming for your class ? Why don't you provide the Java file so we can take a look at it !
hmmm, it seems that both answers (including the one you posted previously) are the suggested route. There maybe something specific to the system I am using that prevents me from doing this. I am using a virtual machine that was set up for me, so I will check with that person to find if there is something else I need to do. Thanks for your help.
You are a life saver!! you know that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.