# Using custom \`main(String\[\] args)\` breaks file reading functions in Windows (e.g. \`loadShader()\`, \`loadImage()\`)

**URL:** <https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572>\
**Category:** Coding Questions\
**Created:** [August 12, 2018, 1:16pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572 "2018-08-12T13:16:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 12, 2018, 1:16pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/1 "2018-08-12T13:16:30Z")

</div>

Hello,

I’m using `loadShader()` and loading a file which is saved to the `data` folder. I changed my sketch to include a custom `main()` function, however the loadShader function doesn’t work after that (Processing 3.3.7, Windows 10 64).  
Any tips on how to make this work? Hardcoding the full path to the shader file works, but obviously not a practical solution.

```auto
static public void main(String[] args) {
  String[] appletArgs = new String[] { "BlurFilter"};
  PApplet.main(appletArgs);
}

```

PS: I raised this issue also in Github ([issue 5615](https://github.com/processing/processing/issues/5615). I doubt I’m the first one to face this problem, therefore I’m cross posting, hoping there is a workaround, by writing the code in a different way.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 12, 2018, 1:35pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/2 "2018-08-12T13:35:58Z")

</div>

AFAIK, either remove **main()** or export the sketch. 😵

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [August 12, 2018, 2:18pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/3 "2018-08-12T14:18:15Z")

</div>

What about using `sketchPath` or `dataPath` ?

```
println(dataPath("file.txt"));
```

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 12, 2018, 2:59pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/4 "2018-08-12T14:59:34Z")

</div>

Exporting the sketch doesn’t make any difference.

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 12, 2018, 3:02pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/5 "2018-08-12T15:02:49Z")

</div>

adding a custom `main()` changes the output of `sketchPath()` to the location of Processing executable:

- Without `main(String[] args)` , `sketchPath("")` returns the location of the `.pde` file: C:\processing-3.3.7\modes\java\examples\Basics\Image\Alphamask\
- With `main(String[] args)` , `sketchPath("")` returns the location of my Processing installation: C:\processing-3.3.7\

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 12, 2018, 3:44pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/6 "2018-08-12T15:44:57Z")

</div>

> [@haschdl](#):
>
> Exporting the sketch doesn’t make any difference.

Well, it used to. Running the exported sketch should use its current folder as its _sketchPath_. 🤔

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [August 12, 2018, 7:28pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/7 "2018-08-12T19:28:18Z")

</div>

Since you were more interested in the `dataPath` where the shaders should be located, did you try that too?

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 12, 2018, 8:28pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/8 "2018-08-12T20:28:59Z")

</div>

hi @GoToLoop  
You are right, in the sense that file loading functions will work, if you export the sketch works _without_ a custom `main(String[] args)` in the sketch

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 12, 2018, 8:30pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/9 "2018-08-12T20:30:22Z")

</div>

pretty much the same thing, `dataPath()` returns Processing installation path, appended with `/data`

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 13, 2018, 1:36am UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/10 "2018-08-13T01:36:08Z")

</div>

> [@haschdl](#):
>
> if you export the sketch works _without_ a custom `main(String[] args)` in the sketch.

Just to make sure if the _sketchPath_ is still recognized in recent Processing versions when we run the exported sketch using a customized **main()**, I’ve coded this simple test sketch below: 🤪

```auto
/**
 * PApplet.main() (v1.0)
 * GoToLoop (2018/Aug/12)
 *
 * Discourse.Processing.org/t/
 * using-custom-main-string-args-breaks-file-reading-functions-
 * in-windows-e-g-loadshader-loadimage/2572/10
 */

public static void main(final String[] args) {
  final String sketch = Thread.currentThread().getStackTrace()[1].getClassName();
  main(sketch, args);
}

@Override public void settings() {
  size(800, 60);
  noLoop();

  println(sketchPath());
  printArray(args);
}

@Override public void draw() {
  clear();
  text(sketchPath(), 20, 20);
  if (args != null) text(join(args, " : "), 20, 40);
}

```

BtW, I’m using most recent 64-bit PDE v3.4 on Win 8.1 to export the sketch above. 💻

I went to the exported’s subfolder “application.windows64/” and double-clicked the “.exe” file there. 📂

And the _sketchPath_ shown there matches the full path where I’m running it! 👌

I can even open a command prompt and run the “.exe” passing typed args to it, and they’re also displayed! 🤩

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 13, 2018, 8:14am UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/11 "2018-08-13T08:14:23Z")

</div>

Hi @GoToLoop, thank you for engaging and for your time testing it. I was wondering, does it matter where you have Processing installed, and where you have your sketches saved?  
Does your example also returns the correct path when running from Processing (before exporting)?

In my computer Processing is installed (=unzipped) at `C:\processing-3.3.7`, which is the path returned by `sketchPath`. My sketches are not under that path, they are at e.g. `c:\Github\RepositoryName\SketchName`

If you someone is interested, here is the [source code for sketchPath](https://github.com/processing/processing/blob/db659cf0fff5d76d535082297d8c0dec6b52386d/core/src/processing/core/PApplet.java#L7845). I didn’t find anything…

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 13, 2018, 8:22am UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/12 "2018-08-13T08:22:03Z")

</div>

> [@haschdl](#):
>
> Does your example also returns the correct path when running from Processing (before exporting)?

My response is the same as my 1st reply to you: 🤓

> [@GoToLoop](#):
>
> AFAIK, either remove **main()** or export the sketch. 😵

My example sketch works correctly when we run its exported “.exe”. Am I clear now? 👍

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 13, 2018, 9:07am UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/13 "2018-08-13T09:07:15Z")

</div>

I understood it works in you setup once you export the app.  
Any idea how to make it work from the Processing environment?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 13, 2018, 9:33am UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/14 "2018-08-13T09:33:28Z")

</div>

By default, field PApplet::_sketchPath_ would point to the “Current Working Directory” (CWD):

> **[Working directory](https://en.wikipedia.org/wiki/Working_directory)**
>
> In computing, the working directory of a process is a directory of a hierarchical file system, if any,\[nb 1\] dynamically associated with each process. It is sometimes called the current working directory (CWD), e.g. the BSD getcwd(3) function, or just current directory. When the process refers to a file using a simple file name or relative path (as opposed to a file designated by a full path from a root directory), the reference is interpreted relative to the current working directory of the p In...

Unless a special _“–sketch-path=”_ argument is passed to PApplet.**runSketch()**:

- [Forum.Processing.org/two/discussion/16457/size-method-for-intial-window-not-working-when-more-windows-are-are-added#Item\_1](http://Forum.Processing.org/two/discussion/16457/size-method-for-intial-window-not-working-when-more-windows-are-are-added#Item_1)

I believe the PDE (Processing’s IDE) creates its own customized **main(String[] args)** which passes all of those custom args to the compiled sketch.

And when we override it by making our own **main(String[] args)**, the PDE can’t pass _“–sketch-path=”_ to our sketch.

And since the CWD is where the PDE is located, our sketch inherits that path. 😖

---

<div class="post-metadata">

**Author:** ![haschdl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/haschdl/32/1082_2.png) [@haschdl](https://discourse.processing.org/u/haschdl)\
**Post date:** [August 14, 2018, 7:47pm UTC](https://discourse.processing.org/t/using-custom-main-string-args-breaks-file-reading-functions-in-windows-e-g-loadshader-loadimage/2572/15 "2018-08-14T19:47:33Z")

</div>

It makes sense! I will try to find another solution to my problem, thanks @GoToLoop
