0

I want to make a tool that can generate and compile java source code and generate a jar file from it:

For this i use the JavaCompiler:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

When i add the classpath to the Java SystemCompiler:

optionList.addAll(Arrays.asList("-cp", classpath));

it works for Windows when i setup my classpath with ";" as seperator:

classpath = "jar1.jar;jar2.jar;dir/jar3.jar;dir/jar4.jar";

In Linux it fails.

When i use space instead of ; like:

classpath = "jar1.jar jar2.jar lib/jar3.jar lib/jar4.jar";

it fails for both systems.

Same goes for:

classpath = "lib/*"

I would need a solution that can generate a working classpath system independently.

UPDATE (Solution):

Ok i found out that there is a java offers

File.pathseparator 

Which changes corresponding to the System Environment.

See also File.separator or File.pathSeparator

2 Answers 2

2

On linux you should use :

CLASSPATH = path1:path2:...

Oracle documentation:

Linux

Windows

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

Comments

1

Use : on linux instead of ; that you are regular to use on windows.

2 Comments

Is there a possibility that i can generate a working CP system independently?
Please clarify your question. Do you mean that you want to create script that runs on any platform and generates classpath following rules of current platform?

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.