25

I created a Java application that sends an HTTP POST request to another server.

  • The firewall in the server is off.
  • The firewall in the PC is off.

The application works perfectly on the PC in the C drive.

The application works perfectly on the PC with a local mapping drive.

But, when I put my application in a network mapping drive, I get an error:

java.net.SocketException: Invalid argument: create in mapped drive

This is my code:

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(getUrl());
postRequest.setHeader(AUTHERIZTION, getAuthString());

HttpResponse response = httpClient.execute(postRequest);

This is the full error:

java.net.SocketException: Invalid argument: create
    at java.net.Socket.createImpl(Unknown Source)
    at java.net.Socket.getImpl(Unknown Source)
    at java.net.Socket.setSoTimeout(Unknown Source)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:119)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at rest.RestClient.postGetQuery(RestClient.java:105)
    at frame.ScanJFrame.initProperty(ScanJFrame.java:195)
    at Main.runAction(Main.java:63)
    at Main.main(Main.java:21)

I tried this:

setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true

But, nothing changed.

What can I do?

Kirby
  • 15,127
  • 10
  • 89
  • 104
kfir
  • 732
  • 10
  • 22
  • 11
    There is an open bug related to your question: [Socket Exception from bind to localhost if JVM installed on a share](https://bugs.openjdk.java.net/browse/JDK-8068568) – Vüsal Feb 19 '19 at 16:31
  • 1
    this may be related to network issue you can use packet tracer to identify where your error occurs – Akash Shah Feb 20 '19 at 11:59
  • 1
    There appears to be a [workaround](https://stackoverflow.com/a/35774170/3080094) for the bug that Vusal mentioned. You could test the workaround to see if the bug is related to your issue. – vanOekel Feb 21 '19 at 13:25
  • Could you please write your ALL command Line to run this APPLICATION ? – KUTAY ZORLU Aug 06 '21 at 18:00
  • //computername is a thought , file:// is also. Other subtleties with http networking include port number of the server you are calling 8080 or 80 if it is an http server, if not then it is file protocol. – Samuel Marchant Aug 11 '23 at 16:48

2 Answers2

0

Hazard a guess, the server has no binding to a port to listen for network external machine connections in either it's own configuration files or to its machine port in its OS configuration "network settings" the external calling PC is using, that and with http it should simply be a domain http url called.

I best also point that the jdk or jre have a "jconsole" program in their /bin directories can be used to set up network connections for the binaries running the caller JVM. This appears To be the problem, your java client program needs a binding to a port to the other machine to connect to it.

If you are calling a file from another machine , use mapped drive syntax of "file://"+"//servername/afolder/file.txt" This scheme is for url's that are mapped drives.

Samuel Marchant
  • 331
  • 2
  • 6
-1

When running your application from a network mapping drive, you may encounter the "java.net.SocketException: Invalid argument: create" error or other similar issues. This problem can arise due to certain limitations or configurations related to network drives.

One possible cause for this error is the restriction on executing files from network drives by default in Java's security settings. By default, Java considers network drives as potentially untrusted locations, and it may restrict certain operations.

To resolve this issue, you can try the following steps:

  1. Adjust Java security settings: Modify the Java security settings to allow the execution of files from network drives. You can do this by going to the Java Control Panel, navigating to the "Security" tab, and adding the network drive path to the "Exception Site List." This tells Java to trust files executed from that location. Keep in mind that modifying security settings should be done cautiously, as it can have implications for other applications as well.

  2. Copy the application to a local drive: If possible, consider copying the application from the network mapping drive to a local drive on the machine where it will be executed. Running the application from a local drive eliminates any potential issues related to network drive restrictions.

  3. Use UNC path: Instead of mapping the network drive, you can try using the UNC (Uniform Naming Convention) path directly in your code. The UNC path uses the network share name to access files, rather than mapping a drive letter. This approach may bypass any restrictions imposed on network drive mappings. Use UNC path instead of the mapped drive,

    String getUrl() {

    return "\server\share\path\to\resource";

    }

  4. Check network drive accessibility: Ensure that the network drive is accessible and that the user running the application has the necessary permissions to read/write to that drive. If there are any issues with the network drive's connectivity or permissions, it may cause errors when trying to access files or resources.

By applying these suggestions, you should be able to overcome the "java.net.SocketException: Invalid argument: create" error when running your application from a network mapping drive.

Arijit
  • 69
  • 1
  • 10
  • Just a heads-up that all five of your answers this month appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. – NotTheDr01ds Jun 11 '23 at 15:24
  • 1
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 11 '23 at 15:24